Two questions (see inside)

K

kelvSYC

How do you iterate through an array of structs? I tried something like
this and it doesn't work (my compiler says "illegal operand"):

struct foo f[10];
for (i = 0; f; i++) {
// Insert code here
}

-------------------

If you have a function like this:

void foo(struct bar *b[]);

How do you call it? Would

struct bar a[10];
foo(a);

work?
 
T

those who know me have no need of my name

in comp.lang.c i read:
How do you iterate through an array of structs?
yes.

I tried something like
this and it doesn't work (my compiler says "illegal operand"):

struct foo f[10];
for (i = 0; f; i++) {


what do you think you are testing here (with the f)? where did you
learn this sort of style? perhaps you learned it from working with
strings, where you would be testing whether a particular byte's value is
non-zero, i.e., terminating the loop when a null byte is encountered. that
makes sense for bytes, but a struct doesn't have a value, only it's members
have values.

#define NELM(array) (sizeof array / sizeof array[0])
for (i = 0; i < NELM(f); i++) {

the NELM macro yields the number of elements in an array. loop termination
is based on the index number not on some magical value of the indexed object.
If you have a function like this:

void foo(struct bar *b[]);

How do you call it? Would

struct bar a[10];
foo(a);

work?

no. you have a prototype that specifies an array of pointers to struct
bar's, but you have an array of struct bar's.

if you want to use the function you have prototyped you need need to
construct something appropriate to pass, e.g.,

struct bar a[10], *pa[NELM(a)];
for (i = 0; i < NELM(a); i++) pa = &a;
foo(pa);

usage in foo() would be `b[n]->member'.

if you want to pass an array to a function that function's prototype would
be:

void foo(struct bar b[]);
or:
void foo(struct bar *b);

usage in this foo would be `b[n].member'.
 
N

Nick Austin

How do you iterate through an array of structs? I tried something like
this and it doesn't work (my compiler says "illegal operand"):

struct foo f[10];
for (i = 0; f; i++) {
// Insert code here
}


for (i = 0; i < 10; i++)
{
/* code that uses f, e.g.: */
f.fi = 0;
}
If you have a function like this:

void foo(struct bar *b[]);

Thats a pointer to an array. Because the array in this context
decays into a pointer this function expects a pointer to a pointer.
How do you call it? Would

struct bar a[10];
foo(a);

You need to create an additional temp variable and pass the
address of it to the function:

struct bar *p;

p = a;
foo(&p);

However, I can't help feeling that you incorrectly declared
the function. It would be simpler as:

void foo( struct bar *b );
or
void foo( struct bar b[] );

Then call it with:

struct bar a[10];
foo(a);

Nick.
 
S

Simon Biber

Nick Austin said:
void foo(struct bar *b[]);

Thats a pointer to an array. Because the array in this context
decays into a pointer this function expects a pointer to a pointer.

No, the declaration of the parameter b specifies an 'array of pointers',
not a 'pointer to array'. The array in this context decays into a
pointer to its first element, therefore it expects a 'pointer to a pointer'.

If it had instead actually been a 'pointer to an array':
void foo(struct bar (*b)[]);
There would be no decaying as the type is not an array type. It would
indeed expect a 'pointer to array'. This can be achieved by passing
an 'array of arrays' by value, which decays to a 'pointer to array'.
 
K

kelvSYC

If it had instead actually been a 'pointer to an array':
void foo(struct bar (*b)[]);
There would be no decaying as the type is not an array type. It would
indeed expect a 'pointer to array'. This can be achieved by passing
an 'array of arrays' by value, which decays to a 'pointer to array'.

I actually meant a pointer to an array but I messed up my operator
precedence. Going back to the original question:

If you had a function,
void foo(struct bar (*b)[]);

and an array,
struct bar a[10];

How would you call the function, and how would the array be referred to
inside the function?
 
K

Kevin Easton

kelvSYC said:
If it had instead actually been a 'pointer to an array':
void foo(struct bar (*b)[]);
There would be no decaying as the type is not an array type. It would
indeed expect a 'pointer to array'. This can be achieved by passing
an 'array of arrays' by value, which decays to a 'pointer to array'.

I actually meant a pointer to an array but I messed up my operator
precedence. Going back to the original question:

If you had a function,
void foo(struct bar (*b)[]);

You can't. You could have this though:

void foo(struct bar (*b)[10]);
and an array,
struct bar a[10];

How would you call the function, and how would the array be referred to
inside the function?

You'd call it with:

foo(&a);

....and refer to it inside the function like (*b)[3].

- Kevin.
 
D

Dave Thompson

kelvSYC said:
If it had instead actually been a 'pointer to an array':
void foo(struct bar (*b)[]);
There would be no decaying as the type is not an array type. It would
indeed expect a 'pointer to array'. This can be achieved by passing
an 'array of arrays' by value, which decays to a 'pointer to array'.

I actually meant a pointer to an array but I messed up my operator
precedence. Going back to the original question:

If you had a function,
void foo(struct bar (*b)[]);

You can't. You could have this though:

void foo(struct bar (*b)[10]);
Yes you can. Array of unknown bound is incomplete, but you can have a
pointer to incomplete. And the call is legal because unknown bound is
compatible with any fixed (or VLA) bound (and the same element type),
and pointers to compatible are compatible. What you can't do is use
this to step through a 2-D array (array of array); for that you need
to specify the lower-rank bound so you have pointer to complete.
and an array,
struct bar a[10];

How would you call the function, and how would the array be referred to
inside the function?

You'd call it with:

foo(&a);

...and refer to it inside the function like (*b)[3].
Yes.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,589
Members
47,212
Latest member
JaydenBail

Latest Threads

Top