Sign in to follow this  
BringBackFuturama

Pointer To Array Of Structs

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 *'

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


struct activity
{
int ID; // or whatever;
};

void fill(activity *ptr,int n,int v)
{
for(int i=0;i<n;++i) ptr[i].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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this