Pointer array and sizeof confusion

pointer is a pointer. It is the size of a pointer, which is 4 bytes on your system.

*pointer is also a pointer. sizeof(*pointer) will also be 4.

**pointer is a char. sizeof(**pointer) will be 1. Note that **pointer is a char because it is defined as char**. The size of the array new`ed nevers enters into this.

Note that sizeof is a compiler operator. It is rendered to a constant at compile time. Anything that could be changed at runtime (like the size of a new'ed array) cannnot be determined using sizeof.

Note 2: If you had defined that as:

char* array[1];
char** pointer = array;

Now pointer has essencially the same value as before, but now you can say:

 int  arraySize = sizeof(array); // size of total space of array 
 int  arrayLen = sizeof(array)/sizeof(array[0]); // number of element == 1 here.

sizeof always returns a number of bytes.

Here, pointer is an ... err ... pointer and is 32 bits on 32 bits architectures, i.e. 4 bytes.


When you call sizeof you're asking for how large it is in terms of bytes. A pointer is actually an integer that represents an address where the data you're pointing to is, and assuming that you're using a x32 operating system the size of an int is 4 bytes.