PDA

View Full Version : arrays of structs




farmerdoug
Feb 6, 2013, 04:47 PM
if maxes is a ** to a struct and sys is a * to the same type of struct
can I do this

maxes[][] = sys[]?

The answer seems to be yes because I tested it but I not sure why. Haven't I violated some pointer rule?

thanks



mfram
Feb 6, 2013, 07:04 PM
You've posted theoretical code which won't compile. Post a real example and maybe we can answer. Keep in mind that you can assign pointers around all you want. But the compiler will not copy entire arrays using an assignment statement.

farmerdoug
Feb 6, 2013, 07:44 PM
It does compile.
Anyway I've stop using that code.
Thanks

Sydde
Feb 7, 2013, 01:47 PM
if maxes is a ** to a struct and sys is a * to the same type of struct
can I do this

maxes[][] = sys[]?

The answer seems to be yes because I tested it but I not sure why. Haven't I violated some pointer rule?

thanks

Not really. The basic rule in C is that a pointer implicitly points to an array of its type. A pointer to a pointer implicitly points to an array of pointers, and each pointer in that array implicitly points to an array of the declared type, so using the doubled subscript notation is perfectly valid.

Note that a function pointer is one pointer type you should obviously never treat as a pointer to an array.