returning an array of pointers???

Started by
18 comments, last by edwinnie 20 years, 11 months ago
quote:Original post by edwinnie
i must say, i am not realli gd at STL yet,
so i kept getting errors like "vector is not a member of 'std'"?
You should get yourself familiar with it; it's really handy. You must have an #include <vector>. Some poeple also use using namespace std; so they don't have to qualify the namespace (std::xxx), but I think it is good to be explicit.
quote:hmm...i might have noticed:

void GetCrystals(std::vector<Crystal *> &crystal_vector);
std::vector<Crystal *> GetCrystals(void);

or

std::vector Player::GetCrystals(void);


hmm... return type is a std::vector? or std::vector<Crystal*>?
I'm not sure what you are asking about here. The first one you could use like this:

std::vector<Crystal *> crystal_list;
player.GetCrystals(crystal_list); // function will "build" your crystal_list
play_area.Generate(crystal_list);

The second return the whole vector object, and could be used like this:
play_area.Generate(player.GetCrystals());

Your alternate approach won't work; you need to specify a type which the vector should hold. In this case Crystal *.
quote:and how come function arguments are different? one is void and the other is std::vector<Crystal*> &crystal_vector?
Alternate approaches. You can either tell the function where to store the pointers, or let the function return them. The latter is cleaner , but is less efficient. In the former case you only need to pass a reference (a pointer) to the function which don't have to return anything, while the latter needs to return an object that is possibly 4 times the size of a pointer.

[edited by - CWizard on May 5, 2003 9:25:17 AM]
Advertisement
quote:Original post by Chigaimasu
Wait a minute... Aren''t arrays global?
No, not unless you declare them global. There is nothing special about arrays in that regard.
quote:Original post by Chigaimasu
Wait a minute... Aren't arrays global? IE, couldn't you do something like this:

Of course, I could be totally wrong.


Curse you windows and your poor explanation of my errors!!!

You are totally wrong.
Why all the STL discussion? Crystal** should work...

[edited by - novum on May 5, 2003 9:27:23 AM]
suppose if i use Crystal** as the return type,
and there is an array of pointers: Crystal* crystals[4];


then if this function has a prototype:
void Generate(Crystal** ptr_To_arrayptrs);

i could call it by using Generate( player->GetCrystals() );

but inside Generate() function,
i need to access "crystal[0], crystal[1], ..."

issit just by stating,
crystal[0]->....
or
&crystal[0]->....
?

thx!

Edwin
quote:Original post by noVum

Why all the STL discussion? Crystal** should work...




He's right, if you don't want to learn STL right now (although I recommend it), Crystal** will work.

Suppose you have a struct Crystal, and classes Player and Play_Area.

crystal has data:
int dummy_value;

Player has data:
int num_crystals;
Crystal* crystals[4]; // Alternatively, Crystal** crystals;

player has functions (assuming you initialize your crystals array):
Crystal** GetCrystals(){ return crystals; }
int GetNumCrystals(){ return num_crystals; }


Play_Area has functions:

void Generate( Crystal** arrayptr_crystals, int num_in_array ){

// Print out the contents of each Crystal
for (int i = 0; i < num_in_array; i++){
printf("Crystals[%d]->dummy_value = %d \n\r",i,arrayptr_crystals->dummy_value);
}}


you can access it by
Generate( player.GetCrystals(), player.GetNumCrystals() )


----
EDIT:

Suppose I'm too slow in the reply.

You access "crystal[0], etc" by

ptr_To_arrayptrs[0]->... (using the above, arrayptr_crystals->dummy_value)


[edited by - KeyS on May 5, 2003 9:40:11 AM]


[edited by - KeyS on May 5, 2003 9:43:32 AM]
alright way to go!
thx!

Edwin
I would argue against returning an array pointer, because there is no (good) way of telling how many elements it contain.

If you don't want to use STL, you could use a simple struct:
typedef struct {    Chrystal *ptr[4];} ChrystalList; // or typedef struct {    Chrystal **list;    int size;} ChrystalList; 
And throw that or a pointer to it around, as it provides a mean to know how many elements are in the array.

EDIT: Actually, KeyS provided a similar way to solve the unknown-number-of-chrystals problem, and perhaps an easier one.

[edited by - CWizard on May 5, 2003 10:07:53 AM]


[edited by - edwinnie on May 5, 2003 10:35:25 AM]
quote:Original post by edwinnie
is there a violation?
i seem to get an unhandled exception,

from:

ptr_To_arrayptrs[0]->... (using the above, arrayptr_crystals->dummy_value)

it seems that "ptr_To_arrayptrs" points to an "array of pointers", so isnt this a violation:

ptr_To_arrayptrs[0]->...


//in order to access crystals[0]
should it be ptr_To_arrayptrs->crystals[0]->...


someone pls clarify.
thx!

Edwin




ptr_To_arrayptrs is Crystal**, or an array of pointers

ptr_To_arrayptrs[0] is Crystal*, because [] dereferences 1 level of pointer

ptr_To_arrayptrs[0]->x is entirely dereferenced because -> dereferences the 2nd and final level of pointer

an unhandled exception might be from not having a valid memory address at "dummy_value" (ptr_To_arrayptrs[#]->dummy_value), did you initialize your array of pointers at all?

Assume you have the struct crystal with 1 int dummy in it, and class Player with Crystal* crystals[4] in it, somewhere in the code (constructor is a good place), you''d need

crystals[0] = new Crystal;
crystals[0]->dummy = 0; // initialization value, could be anything
...(repeat for 1-3)...

Alternatively, you could make Crystal a class and have it''s own constructor so you wouldn''t have to type "crystals[0]->dummy = 0;" for every time you type "crystals[0] = new Crystal;"

Is this your problem?
First of all think about what an array is. All arrays get resolved down to pointers, its just that arrays are easier for some people. So if you want to return a pointer to an array of pointers, simply return the address of the first element(either in array/pointer notation), and the indirection will take care of everything.

This topic is closed to new replies.

Advertisement