Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
struct sample
    char *name ;
int main()
    struct sample sam; = (char *)malloc(sizeof(char)*100);
    return 0;

size of name is showing 4

but i have allocated 100 bytes but it is showing 4 bytes
Updated 11-Apr-13 11:32am

The printed result is correct. The size of a char pointer is 4 bytes on your machine.

If you wanted to print the size of the array the the name pointer is pointing to: That is unfortunately not available as information on a pointer to an array. name is just a pointer to an array of elements of type char and your code has to remember separately how long that array is.

In case you wanted to print how long the null-terminated string in that array is, you would use:
printf ("%d\n", strlen (;
Share this answer
H.Brydon 13-Apr-13 13:23pm    
Close but not quite true. This gives the length of the string not the size.

char ss[6] = "Hello";

strlen(ss) gives 5
sizeof(ss) gives 6

+5 for answering OP question
nv3 13-Apr-13 14:15pm    
Thanks, Harvey. Depends on what you regard as "string length". No doubt, the space the string takes up is length + 1 for the null.

And in our modern days with 64-bit computers, the %d doesn't exactly fit any more to the size_t which is returned by strlen. But I didn't want to confuse this pal by so much sophisticated detail.

Great, you are back!
whenever you run sizeof operator on pointers you get only the size of the pointer i.e 4 bytes in case of windows.
if you really want to get the size of allocated memory you need to follow different approach,
on windows platform you have memory allocation APIs, HeapAlloc, HeapFree and Heapsize.
so instead of using new you can use HeapAlloc, refer MSDN for further details on parameters.
then you can use Heapsize to get actual no of bytes allocated for that memory location.[^]
Share this answer is a pointer to char and, the size of the pointer variable is 4. Did you intend to get the size of the string? (strlen(

Share this answer
H.Brydon 13-Apr-13 13:28pm    
[Same issue as Solution #1]
String length of the name would be strlen( but size would be 1 + strlen( The buffer for the code here is 100 chars but if you were doing a deep copy for example, you need to add 1 to the string length for the minimum size of the 'to' buffer.

Size of a string is not the same as its length (never - not in any case).
Shmuel Zang 14-Apr-13 2:40am    
Of course, we need another character for the null-terminator. But, as nv3 wrote, we don't want to confuse the OP with too much information.
H.Brydon 14-Apr-13 15:06pm    
You said "Did you intend to get the size of the string? (strlen("

strlen() does not produce the size of a string. It produces the string length, which is a different number.
Shmuel Zang 15-Apr-13 4:09am    
You right. I had to write "the length of the string" to be more precise. But, what is the size of the string? (length + 1) is the minimal size required for the buffer that holds the string but, in this case, the buffer's size is 100.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900