en********@gmai l.com wrote, On 25/06/07 23:07:
I have an array of structures (my own typedef) that I wish to pass to
a function.
Since the array with the structs is rather large, I would like to
pass
a pointer to just the first element and have the callee function
recast
it as an array instead of just a pointer to the first element of the
array..
You need to read section 6 of the comp.lang.c FAQ at
http://c-faq.com/
Something like...
typedef struct
{
int structElement;
}myStruct;
myStruct myArray[5];
Why make this a "global"? Why not declare it in main (or where ever it
is needed in the real program).
main()
Implicit int was dropped in C99, the latest (but hardly implemented)
standard. Also it is better to be explicit about lack of parameters.
int main(void)
{
foobar( &(myArray[0]);
You can simplify the above to:
foobar(myArray) ;
You should also ensure a prototype for foobar appears before it is
called. Either prototype it before main or declare it before main. Then
you will get better error checking from your compiler.
}
and..
foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;
which then, ideally, I could index...
Easy. Remove the line above any use array indexing on the pointer.
However, remember that arrays are NOT pointer, and pointers are NOT
arrays. It is just that attempting to pass an array actually passes a
pointer to its first element and you can use array indexing on pointers
and pointer arithmetic on arrays.
val = recastArray[2].structElement;
}
The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?
See above.
--
Flash Gordon