-2

I saw that an array of pointers can be created using vector, however, I don't want that. Is the example below a way to create a pointer to int array?

#include <iostream>
using namespace std;

int main() {
    int* arr[4];
    for (int i=0; i<4; ++i) {
        cout<<endl<<arr[i];
    }
}

This makes a pointer to int array and it displays the memory address of each index in the array. Now I have few questions. Is it a proper way to create a pointer to int array without a vector? Also, if I want to initialize a value inside each memory address in the given example, how is it done? And lastly why is &arr equal to arr?

Brian
  • 14,610
  • 7
  • 35
  • 43
zLeon
  • 137
  • 9
  • 6
    `This makes a pointer to int array` no it does not do that. That is an array of 4 int pointers and accessing any of them like you are doing now is undefined behavior as they are uninitialized. – Borgleader Sep 02 '15 at 17:00
  • Read about: http://stackoverflow.com/questions/1461432/what-is-array-decaying – πάντα ῥεῖ Sep 02 '15 at 17:03
  • Note to zLeon: You left a grammatical trap in your question. "pointer to int array" is ambiguous. It could be interpreted as "(pointer to int) array" or "pointer to (int array)". Your statement "array of pointers" which a context matching your code., but this did not seem to help a number of readers. – user4581301 Sep 02 '15 at 17:39

3 Answers3

5

While &arr and just plain arr may both give you the same address, they are both very different.

With &arr you get a pointer to the array, and the type of it is (in your case) int* (*)[4].

When you use arr it decays to a pointer to the first element and the type is (again, in your case) int**.

Same address, but different types.


As for the array itself, it's defined fine, you have an array of four pointers to int. However, you do not initialize the contents of the array, which means that the contents is indeterminate, and using those pointers in any way (even just printing them) leads to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    To those reading this answer and thinking, "Duh... An address will be printed. Behaviour quite well defined." Please note the standard does not specify what address will be printed since it hasn't been assigned, and that is most definitely undefined behaviour. You may have systems that automatically NULL the pointer, you may have it displaying whatever garbage was on the stack, and it may point to the Unabridged Works of William Shakespeare. All would be acceptable interpretations. – user4581301 Sep 02 '15 at 17:31
1

Your proposed way doesn't make pointer to int array. Instead of that it makes a pointer to pointer to an int array. Usually the name of any array represent a pointer to it self. Or &arr[0] also represent it.

So I hope that you got the answer for why &arr equal arr.

Creating a pointer to int array

int arr[4];
int* p = arr; //pointer to int array

Initializing each element in array

(1) Using pointer arithmetic

int size = 4;
int* p = arr;
for (int i = 0; i < size; i++)
{
    *p = i; // assigning each element i
    p++; //pointing to next element 
}

(2) Using operator []

int size = 4;
for (int i = 0; i < size; i++)
{
    arr[i] = i; // assigning each element i
}
Amith Chinthaka
  • 1,015
  • 1
  • 17
  • 24
0

&arr gives you the address of array which starts with base address ie address of first element.

arr gives the address of first element.

hence u get same result for both

DAVE
  • 125
  • 1
  • 10