pass a pointer to array of structs to functions
Author |
Message |
Stig Brautase #1 / 6
|
 pass a pointer to array of structs to functions
Hi there, I am having immense trouble with a small detail.. I have an array of structures declared thus: struct foo bar[4]; I want to pass the address of this to a function f, so that I in f can do ``bar[1]->member = 1;'' to access bar[1]'s members. I have tried many declarations for f, including this one, that I believe is the correct one: void f(struct foo (*bar)[4]) { Quote: }
Please can anybody confirm this? Stig -- brautaset.org
|
Sun, 31 Oct 2004 07:18:03 GMT |
|
 |
Tobias Oe #2 / 6
|
 pass a pointer to array of structs to functions
Quote:
> Hi there, > I am having immense trouble with a small detail.. I have an array of > structures declared thus: > struct foo bar[4]; > I want to pass the address of this to a function f, so that I in f can > do ``bar[1]->member = 1;'' to access bar[1]'s members. > I have tried many declarations for f, including this one, that I believe > is the correct one: > void f(struct foo (*bar)[4]) > { > }
How would you do it for an array of ints? void f(int *a,int n){ /* n is the number of elements in the array. it's unused here * but is generally needed in the function. */ a[0]=9; Quote: }
int main(void){ int a[10]={0}; f(a,10); printf("%d\n",a[0]); Quote: }
For an array of structs it's the same thing void f(struct foo *bar,int n){ bar[0].i=3; /* suposing there is a member i in a struct bar */ Quote: }
easy! Tobias.
|
Sun, 31 Oct 2004 07:29:02 GMT |
|
 |
Svant #3 / 6
|
 pass a pointer to array of structs to functions
Quote: > Hi there, > I am having immense trouble with a small detail.. I have an array of > structures declared thus: > struct foo bar[4]; > I want to pass the address of this to a function f, so that I in f can > do ``bar[1]->member = 1;'' to access bar[1]'s members. > I have tried many declarations for f, including this one, that I believe > is the correct one: > void f(struct foo (*bar)[4]) > { > } > Please can anybody confirm this?
Yes, No or Maybe... It depends on how you provide the actual parameter, and what you want to achieve. If you call f with f(bar) I would try: void f(struct foo *bar) { Quote: }
or void f(struct foo bar[]) { Quote: }
or even (but this I think is bad style): void f(struct foo bar[4]) { Quote: }
Remember that when using an array name as an actual parameter, it is converted to a pointer to the first element of the array. What is the first element of: struct foo bar[4]; ? It's a 'struct foo'. Thus we get a pointer to struct foo, and that is what must be declared as the formal parameter of the function. A declaration of a parameter as an array of type is adjusted to a pointer to type. As you cannot pass an array to a function, you normally must pass the size of the array as an explicit extra parameter. A way around this, is to pass the pointer to the array object instead, this is easily done with a typedef: typedef struct foo { /* */ Quote: } foo[4];
void f(foo *bar) { Quote: }
int main(void) { foo bar; f(&bar); Quote: }
or, equivalently, without typedefs: struct foo { /* */ Quote: };
void f(struct foo (*bar)[4]) { Quote: }
int main(void) { struct foo bar[4]; f(&bar); Quote: }
-- /Svante http://axcrypt.sourceforge.net Free AES Point'n'Click File Encryption for Windows 9x/ME/2K/XP
|
Sun, 31 Oct 2004 07:54:15 GMT |
|
 |
Stig Brautase #4 / 6
|
 pass a pointer to array of structs to functions
Hi Tobias, Quote:
[ ... ] >> I am having immense trouble with a small detail.. I have an array of >> structures declared thus: >> struct foo bar[4]; >> I want to pass the address of this to a function f, so that I in f can >> do ``bar[1]->member = 1;'' to access bar[1]'s members. > How would you do it for an array of ints? [ ... ] > For an array of structs it's the same thing > void f(struct foo *bar,int n){ > bar[0].i=3; /* suposing there is a member i in a struct bar */ > } > easy!
You're right, it was :D Thanks for a quick answer, I was about to go crazy over this one.. Probably me just being stressed, since I have to have this game finished by Thursdag... :( Stig -- brautaset.org
|
Sun, 31 Oct 2004 08:08:21 GMT |
|
 |
Barry Schwar #5 / 6
|
 pass a pointer to array of structs to functions
Quote: >Hi there, >I am having immense trouble with a small detail.. I have an array of >structures declared thus: >struct foo bar[4]; >I want to pass the address of this to a function f, so that I in f can >do ``bar[1]->member = 1;'' to access bar[1]'s members. >I have tried many declarations for f, including this one, that I believe >is the correct one: >void f(struct foo (*bar)[4]) >{ >} >Please can anybody confirm this? >Stig
If you have an array x of some type T and you pass it to a function with a statement like func(x), then the function must be defined as either func(T *y); or func(T y[]); Since this is adequate for almost every purpose, you reference the individual elements of the array in the function as y[i]. This has type T. In your case, T is struct foo. Therefore you would use y[i].member. If you really, really need to pass the address of the array, as in func(&x), then you define the function as you describe. However, it still doesn't do what you want. In your function, bar is a pointer to an array of 4 struct. Therefore bar[i] is the i-th such array. That is, bar[i] is an array of 4 struct. bar[i][j] is the j-th struct in that array. You would have to use bar[i][j].member to refer to a member. The only way you can use bar[i]->member is if bar[i] is a pointer to struct which requires that bar be either a pointer to pointer to struct (as in struct foo **bar) or that bar be an array of pointers (as in struct foo *bar[]). However, your opening statement says you have an array of struct, not an array of pointers to struct so you probably don't really want this after all. The real question becomes why don't you want bar[i].member which is the "normal" situation? <<Remove the del for email>>
|
Sun, 31 Oct 2004 08:49:53 GMT |
|
 |
Stig Brautase #6 / 6
|
 pass a pointer to array of structs to functions
Hi Barry, Thanks for your explanatory answer.
[snip] Quote: >>struct foo bar[4]; >>I want to pass the address of this to a function f, so that I in f can >>do ``bar[1]->member = 1;'' to access bar[1]'s members.
[snip] Quote: > Since this is adequate for almost every purpose, you reference the > individual elements of the array in the function as y[i]. This has > type T. In your case, T is struct foo. Therefore you would use > y[i].member.
[snip] Quote: > The real question becomes why don't you want bar[i].member which is > the "normal" situation?
Because I am stressed, and I easily get confused when I'm stressed... ;) Stig -- brautaset.org
|
Sun, 31 Oct 2004 10:08:55 GMT |
|
|
|