aPtr
is an array of pointers to char. So each element of aPtr
is a single pointer to char
, or char *
. In particular, aPtr[0]
is a char *
. So sizeof(aPtr[0])
gives you the size of that pointer. On many machines (on "32 bit machines"), the size of a pointer is 32 bits, or 4 bytes.
Based on your question, it sounds like you're interested in the size of the data the pointer points to. That's completely different. (Again, realize carefully the distinction we're making between the pointer and the pointed-to data.) And in this case, finding the size of the pointed-to data can be tricky, and it turns out it probably won't involve the sizeof
operator at all.
If you want to find out the size of a malloc'ed block of memory, it turns out there's no portable way to do that. That is, after you say p = malloc(20)
, there's no way to ask "Hey, malloc
. That pointer you gave me, that I asked you to allocate some memory for, how much memory did I ask you to allocate, again? I forget." If you need to know how much memory your malloc'ed pointers point to, you have to remember it somehow.
If you want to find out how long a string is, on the other hand, that's simple: just call strlen
. For example, a reasonable fix to your program is to have the last line be
printf("%d", strlen(aPtr[0]));
Actually, since strlen
(like sizeof
) returns a special type called size_t
that might be bigger than an int
, the more portable way to write this would be
printf("%zu", strlen(aPtr[0]));
or
printf("%d", (int)strlen(aPtr[0]));
But going back to your original program, there's another misunderstanding we should talk about. You said
aPtr[0] = (char *)malloc(20*sizeof(char));
aPtr[0] = "This is a test";
Now, I know you've been taught that memory allocation is important, and that's true, but in this case, it turns out you're allocating 20 bytes of memory and then throwing it away. Look closely: malloc
returns a pointer value (pointing to the newly-allocated memory), and you assign it to (you store the value in) aPtr[0]
. But then, in the very next line, you assign aPtr[0]
to a different pointer, pointing to the statically-allocated string "This is a test"
. The previous value, the one returned by malloc
, is lost.
What you probably meant to write here was
aPtr[0] = malloc(20);
strcpy(aPtr[0], "This is a test");
Now we're doing something completely different with that string constant "This is a test"
. Instead of simply assigning a pointer to it, we're copying the while string (actually the library function strcpy
is copying the whole string) byte-by-byte into the pointer pointed to by aPtr[0]
, that is, the pointer you just got back from malloc
. (In this latest example I have also dropped the unnecessary cast of malloc
's return value to (char *)
, and the unnecessary multiplication by sizeof(char *)
.)
Notice that in this case (now that you're actually using the malloc'ed memory), the size of the malloc'ed memory matters. If you had said
aPtr[0] = malloc(10);
strcpy(aPtr[0], "This is a test");
then you'd have a big problem.
Finally, there's one other answer to the question, "How big is the string?", although it's not usually too useful, and it's certainly not useful in this case. But if you want to you can write
printf("%zu\n", sizeof("This is a test"));
and this will print 15. (It prints one more than what you thought the string length was, because the count of the size of the string constant includes the terminating \0
character.)