views:

864

answers:

5

Hi, I have two questions:

1) How can I make an array which points to objects of integers?

int* myName[5];  // is this correct?

2) If I want to return a pointer to an array, which points to objects (like (1)) how can I do this in a method? ie) I want to impliment the method:

int **getStuff() {
// what goes here?
return *(myName); // im pretty sure this is not correct
}

Thanks for the help!

+5  A: 

How can I make an array which points to objects?

int * myName[5]; /* correct */

If I want to return a pointer to an array, which points to objects (like (1)) how can I do this in a method?

Technically, you write this function:

int * (* getStuff() )[5] {
    return &myName;
}

That returns a pointer to that array. However, you don't want to do that. You wanted to return a pointer to the first element of the array:

int ** getStuff() {
    return myName; /* or return &myName[0]; */
}

That way, you can now access items as you want like getStuff()[0] = &someInteger;

Johannes Schaub - litb
Voted up, but remember not to return pointers to things that aren't guaranteed to live after the function returns (i.e., local objects living on the stack).
Max Lybbert
+1  A: 

1) Correct - this is an array of 5 pointers to ints

2) You can return a pointer to an array of pointers to ints by returning a pointer to the first element of that array. This has two levels of indirection, so you need two asterisks. You can also return the array normally, since arrays automatically decay into pointers to their first elements.

int **getStuff() {
    return myName;       // 1
    return &myName[0];   // 2
}
Adam Rosenfield
Voted up, but remember not to return pointers to things that aren't guaranteed to live after the function returns (i.e., local objects living on the stack).
Max Lybbert
+1  A: 
int **myName;

int **getStuff() {
  int **array = new int*[5];

  for (int i = 0; i < 5; i++)
  {
    int key = i;
    array[i] = &key;
  }

  return array;
}
Aistina
Voted up, but remember not to return pointers to things that aren't guaranteed to live after the function returns (i.e., local objects living on the stack).
Max Lybbert
+2  A: 

Note that your code,

int* myName[5];

declares an array containing 5 values, each of which is a "pointer to int", which is what you asked.

However this being C++, that's all it does. As a Python scripter, that might cause you some surprises.

It does not give any of those 5 pointers sensible values, and it does not create any integers for them to point to.

If you put it in a function body, then it creates the array on the stack. This means that the array will cease to exist when the current scope ends (which, to put it simply, means when you get to the enclosing close-curly, so for example return does it). So in particular, the following code is bad:

int **myFunction() {
    int *myArray[5];
    return myArray;
} // <-- end of scope, and return takes us out of it

It might compile, but the function returns a pointer to something that no longer exists by the time the caller sees it. This leads to what we call "undefined behaviour".

If you want the array to exist outside the function it's created in, you could create one on the heap each time your function is called, and return a pointer, like this:

int **myFunction() {
    int **myArray = new int[5];
    return myArray;
}

The function returns a different array each time it's called. When the caller has finished with it, it should destroy the array, like this:

delete[] myArray;

otherwise it will never be freed, and will sit around using up memory forever (or when your program exits on most OSes).

Alternatively, you can use the keyword "static" to create an array with "global storage duration" (meaning that it exists as long as the program is running, but there's only one of it rather than a new one each time). That means the function returns the same array each time it's called. The caller could store some pointers in it, forget about it, call the function again, and see the same pointers still there:

int **myFunction() {
    static int *myArray[5];
    return myArray;
}

Note how similar this code is to the very bad code from earlier.

Finally, if you just want to create an array of integers, not an array of pointers to integers, you can do this:

int myArray[5] = { 1, 2, 3, 4, 5};

That actually creates 5 integers (meaning, it assigns space which can store the integer values themselves. That's different from the array of pointers, which stores the addresses of space used to store integer values).

It also stores the specified values in that space: myArray[0] is now 1, myArray[1] is 2, etc.

Steve Jessop
A: 

onebyone.livejournal.com, I think you meant:

int **myFunction() {
    int **myArray = new int*[5];
    return myArray;
}
imran.fanaswala