# Thread: Array of pointers to a struct

1. ## Array of pointers to a struct

Could someone try to explain to me how to make an array of pointers to a structure?

I am trying to create a bunch of structures and store my access to them by storing the pointer to them in an array.

Really confused as to how to do this.

2. It would probably be easier to just make an array of structs... something like

Code:
```struct tStuff
{ int x;
int y;
char z[100];
}

struct tStuff MyStuff[500];```

3. Normally I would (Which is why I don't really understand this lol), but I need to move the pointer around. I hit a brick wall in not knowing how to move forward

4. Originally Posted by Magical
Normally I would (Which is why I don't really understand this lol), but I need to move the pointer around. I hit a brick wall in not knowing how to move forward
Ok do some searching for tutorials on linked lists... You'll also find tons of threads on them here so you may want to do a little local snooping as well.

5. I know linked lists already :/ Not sure how they relate to making an array hold pointers.

I feel like I'm missing something basic lol.

6. Try something like

Code:
```struct tStuff
{ int x;
int y;
char z[100];
}

struct tStuff *MyStuff[500];```
This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.

7. Originally Posted by TheBigH
Try something like

Code:
```struct tStuff
{ int x;
int y;
char z[100];
}

struct tStuff *MyStuff[500];```
This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.
And... don't forget, each one will have to be created with malloc() and destroyed with free().

8. Originally Posted by TheBigH
Try something like

Code:
```struct tStuff
{ int x;
int y;
char z[100];
}

struct tStuff *MyStuff[500];```
This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.
Code:
```MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
1 = MyStuff[i]->x;
2 = MyStuff[i]->y;
3 = MyStuff[i+1]->x;```
Is this close to how I would do it? It feels like this is more of a Struct of Arrays that I could of just declared as

Code:
`struct tStuff MyStuff[500];`
Trying to figure out how to move the pointer to the structs on and off queues.

9. Yes, that's more or less right. Except, do
Code:
`MyStuff[i+1]->x = 3`
rather than the other way round. Also, you have to remember to malloc each element of the array:
Code:
```for( i=0;i<500;i++) {
MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
}```
You are also right in saying that you could just put the structs in the array rather than pointers to the structs. This may well be simpler. However, there are times when an array of pointers to structs is useful. For example, suppose the struct had 100 elements, that one of the elements was a person's surname and that you wanted to sort the structs in alphabetical order of surnames. Sorting involves a lot of swapping, and moving structs around is a lot slower than moving pointers around because pointers are a lot smaller. So the sorting will go faster if your array contains pointers rather than the whole thing.

10. Originally Posted by Magical
Code:
```MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
1 = MyStuff[i]->x;
2 = MyStuff[i]->y;
3 = MyStuff[i+1]->x;```
I'm pretty sure the general principles of math don't allow you to redefine constants abd I'm certain C isn't going to let you tell it 3 doesn't equal 3.

Also there is no reason to typecast the return value of malloc...
Code:
`Mystuf[i] = malloc(sizeof(tStuff));`

11. Originally Posted by CommonTater
I'm pretty sure the general principles of math don't allow you to redefine constants abd I'm certain C isn't going to let you tell it 3 doesn't equal 3.

Also there is no reason to typecast the return value of malloc...
Code:
`Mystuf[i] = malloc(sizeof(tStuff));`
Heehee I typed that fast, meant it to be other way
MyStuff[i+1]->x = 3;

`struct tStuff MyStuff[500];`