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.
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.
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];
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
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.
Try something like
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:struct tStuff { int x; int y; char z[100]; } struct tStuff *MyStuff[500];
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 asCode:MyStuff[i] = (tStuff *) malloc(sizeof(tStuff)); 1 = MyStuff[i]->x; 2 = MyStuff[i]->y; 3 = MyStuff[i+1]->x;
Trying to figure out how to move the pointer to the structs on and off queues.Code:struct tStuff MyStuff[500];
Yes, that's more or less right. Except, do
rather than the other way round. Also, you have to remember to malloc each element of the array:Code:MyStuff[i+1]->x = 3
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.Code:for( i=0;i<500;i++) { MyStuff[i] = (tStuff *) malloc(sizeof(tStuff)); }
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...
is perfectly adequate.Code:Mystuf[i] = malloc(sizeof(tStuff));
It's an array of structs... not the other way around.
Yer a real "Wrongway Feldman" aren't ya ...
And yes it will probably be easier to just declare an array of structs as I showed you right up front. The only restriction being that if you are declaring the array inside a function (such as main() ) you have to be careful how big it gets since it's created on the stack and might cause problems if the stack overflows. Of course the answer is to declare it globally (outside any function) in which case it can be any size you need.