Pointer To Array Of Structs

Started by
5 comments, last by PiCkLeD 18 years ago
Alright, I'm finally on C++, and I need help figuring out how to represent an array of structs using pointers. Lets say I've got a struct called activities, and I want one called x with 100 elements. How would I set up a pointer to pass this struct to a function from one it's defined in, and how would I access it then using pointers? I've got something like this:
[SOURCE]

...
main()
{
	struct activity x[LIST_SIZE];
	struct activity [LIST_SIZE] * y  = &x;
        FillArray((struct activity [LIST_SIZE] *)y)
        ....
}

FillArray(struct activity [LIST_SIZE] * x)
{
       int i;
       for(i = 0; i < 100; ++i)
            *(x+i)->member = (WHATEVER VALUE);
}

[/SOURCE]
Anyway, with something similar to that the compiler is telling me that I can't do this: main.cpp: In function `int FillArray(activity**)': main.cpp:24: request for member `inode' in `*(x + (+(i * 4)))', which is of non-aggregate type `activity*' Please help me understand what I'm doing wrong. I'm completely lost.
Originality is dead.
Advertisement
I've also tried another syntax...which didn't work either:


[SOURCE]...main(){	struct activity x[LIST_SIZE];	struct activity * y  = x;        FillArray((struct activity *)y);}FillArray(struct activity * x){       int i;       for(i = 0; i < LIST_SIZE; ++i)            *(x+i)->MEMBER = SOMEVALUE....[/SOURCE]


It's now giving me:

main.cpp:24: invalid type argument of `unary *'
Originality is dead.
If MEMBER is a pointer inside the struct, this should be

*((x+i)->MEMBER) = SOMEVALUE


else you should use

(x+i)->MEMBER = SOMEVALUE


The explicit cast to 'struct activity*' inside the function call isn't necessary.
Quote:Original post by Anonymous Poster
If MEMBER is a pointer inside the struct, this should be

*** Source Snippet Removed ***

else you should use

*** Source Snippet Removed ***

The explicit cast to 'struct activity*' inside the function call isn't necessary.


Okay...but shouldn't I use a dereference to access the value of the member in the struct? When I try that it says that I'm not allowed to use the * operator.
Originality is dead.
struct activity{    int ID; // or whatever;};void fill(activity *ptr,int n,int v){    for(int i=0;i<n;++i) ptr.ID=v;}int main(){    activity x[20]; fill(x,20,0);}


In C++, you don't need to prefix activity with struct once it has been defined. You don't need to assign the address of x or cast it. In the example above, x is treated like a pointer to an array of structs. And if you apply [] to a pointer, it dereferences it.

Oh, and prefer vector<activity> x over an array.

[Edit] Actually, the last line about vector was a bit harsh for a local stack-based array.

[Edited by - EasilyConfused on April 25, 2006 3:23:12 AM]
Quote:Original post by BringBackFuturama
Quote:Original post by Anonymous Poster
If MEMBER is a pointer inside the struct, this should be

*** Source Snippet Removed ***

else you should use

*** Source Snippet Removed ***

The explicit cast to 'struct activity*' inside the function call isn't necessary.


Okay...but shouldn't I use a dereference to access the value of the member in the struct? When I try that it says that I'm not allowed to use the * operator.


The -> operator deferences it.
Anon Poster, it might be best you do not post when you clearly do not know what you are talking about :)

-> is not applicable and EasilyConfused is correct.

This topic is closed to new replies.

Advertisement