Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing an array of Struct elements

enliteneer@gmail.com
Guest
 
Posts: n/a
#1: Jun 25 '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..


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?


Flash Gordon
Guest
 
Posts: n/a
#2: Jun 25 '07

re: Passing an array of Struct elements


enliteneer@gmail.com wrote, On 25/06/07 23:07:
Quote:
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/
Quote:
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).
Quote:
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)
Quote:
{
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.
Quote:
}
>
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.
Quote:
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
Default User
Guest
 
Posts: n/a
#3: Jun 25 '07

re: Passing an array of Struct elements


enliteneer@gmail.com wrote:
Quote:
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.
Quote:
>
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);
Quote:
}
>
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.
Quote:
>
which then, ideally, I could index...
val = recastArray[2].structElement;
Skip that, use pElement[2].structElement;
Quote:
}
>
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
Closed Thread