How I pass a member of a struct to a function? I can't pass the whole struct because this isn't one of my own functions. Any ideas?
Printable View
How I pass a member of a struct to a function? I can't pass the whole struct because this isn't one of my own functions. Any ideas?
As you pass a normal variable
Code:struct my_struct {
int x;
int y;
};
int do_stuff(int x) {
return 0; /* hey who said it was a useful function */
}
int main(void) {
struct my_struct n = {1, 2};
do_stuff(n.x);
return 0;
}
thanks for the replies, but more specifically how do I pass an array of chars in a struct to a function?
Same idea...but I'm in a helpful mood today.
Code:
#define NAME_SIZE 8
struct my_struct {
char name[NAME_SIZE];
};
int do_stuff(char *name) {
printf("what is your name?");
fgets(name, NAME_SIZE, stdin);
return 0; /* hey who said it was a useful function */
}
int main(void) {
struct my_struct n;
do_stuff(n.name);
printf("My name is %n, and I'm addicted to programming.", n.name);
return 0;
}
Thanks a lot. Must be something wrong with my code then but that's WINAPI - another story....
I am plenty familiar with the WinAPI if you had a more specific question.