Passing an array of Struct elements

E

enliteneer

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..


Something like...

typedef struct
{
int structElement;
}myStruct;

myStruct myArray[5];

main()
{
foobar( &(myArray[0]);
}

and..

foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;

which then, ideally, I could index...
val = recastArray[2].structElement;
}

The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?
 
F

Flash Gordon

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).

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.
 
D

Default User

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're badly confused about arrays and pointers. It's not possible to
pass an array of any kind, all you CAN do is pass a pointer to the
first element. However, there's no casting to be done afterwards, as
you can't cast a pointer to an array.
Something like...

typedef struct
{
int structElement;
}myStruct;

myStruct myArray[5];

main()
{
foobar( &(myArray[0]);

You can do this, but it's more common to do:

foobar(myArray);
}

and..

foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;

This is not possible, pElement is not a correct intializer for an
array. It's also not needed.
which then, ideally, I could index...
val = recastArray[2].structElement;

Skip that, use pElement[2].structElement;
}

The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?

You can't, but don't need to. You COULD, if you really wanted to,
create an array and copy pElement to it, but that's rather pointless.

Please read the FAQ on pointers and arrays.

<http://c-faq.com/>




Brian
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top