| | |
array of structs
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 98
Reputation:
Solved Threads: 2
I have a struct. I want to declare a pointer to that struct. Now I want 10 of those structs. How is this declared? I have tried (call the struct foo for example):
foo* test[10];
when I make a call to test[0]->whatever or test[5]->whatever it seems to always write to the first element (the 0th of the array).
I think what I'm getting is an array of pointers. I want a pointer to an array of structs.
How do I delare this? Thanks.
foo* test[10];
when I make a call to test[0]->whatever or test[5]->whatever it seems to always write to the first element (the 0th of the array).
I think what I'm getting is an array of pointers. I want a pointer to an array of structs.
How do I delare this? Thanks.
Last edited by Nemoticchigga; Mar 26th, 2009 at 11:17 am.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Correction: you are getting an array of uninitialized pointers. Accessing them may result in undefined behaviour. So do it like MrSpigot recommends or use new to initialize the array members.
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Mar 2009
Posts: 109
Reputation:
Solved Threads: 12
you can also do it like Jenca's second suggestion like:
c++ Syntax (Toggle Plain Text)
foo *test; foo = new struct foo[10];
Last edited by SeeTheLite; Mar 26th, 2009 at 4:35 pm.
•
•
•
•
Thanks all.
MrSpigot, how would I then access the structs through the pointer?
pFoo->test[0].index = blah; does not work. How would this be done?
Thanks.
will access the first struct, but more generally you could use pFoo[n].index = blah;
Or modify pFoo:
pFoo += 1;
pFoo->index = blah; // pFoo now references the second struct
Last edited by MrSpigot; Mar 26th, 2009 at 6:18 pm.
![]() |
Similar Threads
- help passing an array of structs to a function? (C++)
- passing an array of structs from a member function (C++)
- Programming student find distances btwn cities using array of structs (C++)
- contact info in array of structs (C++)
- working with array of structs (C++)
- Question about putting structs into arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Iterators act like numbers?
- Next Thread: vector fanction
Views: 360 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





