#167619 - intel3 - Fri Mar 20, 2009 4:54 pm
Hi
I am getting confused with the way how array is being used in c++ as a returned variable
Here is how I declared it:
I want to return the array and here is the function call:
I tried returning beam and it didn't work. So I returned beam[0] and it worked. Is it suppose to be returning beam[0] and why.
Afterward returning the pointer how do I reference other index (for example) like so?
Here are some of the other question I have in mind about pointer and array,
what is the concept behind this and how to use it properly?
In this case why can't I just use char pointer instead seems like it is just pointing to a memory location?
When I declare
Does a pointer to the array also means the pointer starting at index 0? And since it is a pointer does it mean that for example beam[3] is also an pointer and I can declare a new pointer like so:
Thanks for your explaination
I am getting confused with the way how array is being used in c++ as a returned variable
Here is how I declared it:
Code: |
Ship::Ship() { Laser * beam[10]; for(int i = 0; i < 10; i++) beam[i] = new Laser(); } |
I want to return the array and here is the function call:
Code: |
Laser * Ship::getlasers(){ //what should it be? } |
I tried returning beam and it didn't work. So I returned beam[0] and it worked. Is it suppose to be returning beam[0] and why.
Afterward returning the pointer how do I reference other index (for example) like so?
Code: |
Laser * las = ship->getLasers(); las[5] = 3; |
Here are some of the other question I have in mind about pointer and array,
what is the concept behind this and how to use it properly?
In this case why can't I just use char pointer instead seems like it is just pointing to a memory location?
When I declare
Code: |
Laser * beam[10]; or Laser *beam[] = new Laser[10]; |
Does a pointer to the array also means the pointer starting at index 0? And since it is a pointer does it mean that for example beam[3] is also an pointer and I can declare a new pointer like so:
Code: |
laser * one = beam[3]; |
Thanks for your explaination