0

Can you please explain what this piece of C++ code does:

int main()
{
    long * tempArray[10];
}

Thanks for your help!

Shivanshu Goyal
  • 1,374
  • 2
  • 16
  • 22

3 Answers3

1

That particular snippet of code does nothing at all. If you compile that program, it will terminate and report to the hosting environment that it terminated successfully, and that's it.

The long * tempArray[10]; declares a variable called tempArray to have the type array 10 of pointer to long, which means that tempArray is capable of holding 10 long * objects.

For demonstrative purposes:

// declare some long integers
long myLong = 100;
long anotherLong = 400;
long thirdLong = 2100;

// declare array 3 of pointer to long
long *tempArray[3];

// remember that arrays in C and C++ are 0-based, meaning index 0
// refers to the first object in an array. Here we are using the `&'
// operator to obtain a long pointer to each of the longs we declared
// and storing these long pointers in our array.
tempArray[0] = &myLong;
tempArray[1] = &anotherLong;
tempArray[2] = &thirdLong;
dreamlax
  • 93,976
  • 29
  • 161
  • 209
0

It creates an array of 10 long pointers.

Anthony
  • 12,177
  • 9
  • 69
  • 105
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

The code doesn't actually do anything.

long * tempArray[10]; allocates an array capable of holding ten pointers to long integers.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50