Since you are interested in knowing the addresses returned by malloc() , you need to make sure that you type them correctly. Correctly, I mean that for printing addresses you should use the correct format specifier for printf() . Why are you using "%u" for one and "%d" for the other?
You must use "%p" to print pointers. This is also one of the rare cases when you need cast in C: because printf() is a variational function, the compiler cannot say that the pointers you pass as an argument must be of type void * or not.
In addition, you should not specify the return value of malloc() .
Fixed above, program:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char **stringArr; int size=4, i; stringArr = malloc(size * sizeof *stringArr); for (i=0; i < size; i++) stringArr[i] = malloc(10 * sizeof *stringArr[i]); strcpy(stringArr[0], "abcdefgh"); strcpy(stringArr[1], "good-luck"); strcpy(stringArr[2], "mully"); strcpy(stringArr[3], "stam"); for (i=0; i<size; i++) { printf("%s\n", stringArr[i]); printf("%p %p\n", (void *)(&stringArr[i]), (void *)(stringArr[i])); } return 0; }
and I get the following output at startup:
abcdefgh 0x100100080 0x1001000a0 good-luck 0x100100088 0x1001000b0 mully 0x100100090 0x1001000c0 stam 0x100100098 0x1001000d0
On my computer, char ** pointers are 8 bytes long, so &stringArr[i+1] is 8 bytes longer than &stringArr[i] . This is guaranteed by the standard: if you malloc() some space, this space is contiguous. You have allocated space for 4 pointers, and the addresses of these four pointers are located next to each other. You can see this more clearly by doing:
printf("%d\n", (int)(&stringArr[1] - &stringArr[0]));
This should print 1.
About the subsequent malloc() s, since each stringArr[i] obtained from a separate malloc() , the implementation can freely assign them any suitable addresses. In my implementation, with this particular launch, all addresses are divided by 0x10 bytes.
For your implementation, it seems that char ** pointers are 4 bytes long.
About your individual string addresses, it looks like malloc() is doing some sort of randomization (which is allowed to be done).