Can you please explain what this piece of C++ code does:
int main()
{
long * tempArray[10];
}
Thanks for your help!
Can you please explain what this piece of C++ code does:
int main()
{
long * tempArray[10];
}
Thanks for your help!
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;
The code doesn't actually do anything.
long * tempArray[10];
allocates an array capable of holding ten pointers to long integers.