| | |
array of structs
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





