-2

I started learning C and got a question that I could not find the answer to.

When I initialize a random string with

char *str1 = "Big";
printf("size of str1: %ld\n", sizeof(str1));

it gives me the size 8 (bytes) when I do sizeof(str1), which does not makes sense to me.

When I initialize a random string with

char str2[] = "Big";
printf("size of str2: %ld\n", sizeof(str2));

it gives me the size 4 (bytes) when I do sizeof(str2), which DOES make sense to me because three chars 'B', 'i', 'g', and '\0'.

How come the first one gives me the size 8?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
js9
  • 131
  • 7
  • 1
    J - while I didn't downvote, I can surmise the reason is because this is a fairly basic question easily answerable with minimal effort either searching S.O. or the web in general. Don't worry, it isn't the end of the world. But, going forward, make sure you exercise reasonable effort before asking a question if you want to avoid further downvotes. S.O. is here to help, but it shouldn't be used to avoid basic reading or learning. Good luck and welcome to S.O. – David C. Rankin Dec 29 '15 at 06:05
  • David - Thank you, I will try harder to find things on my own next time. I am very new to here and I didn't even know I was downvoted...haha. Sorry everyone. – js9 Dec 29 '15 at 06:10
  • Don't worry about the downvote, we have all had our share. This is a fantastic site, and we all do our part to make it as useful as possible. The vote/reputation system just adds a bit of ... integrity ... to the site. Again, welcome aboard. – David C. Rankin Dec 29 '15 at 06:19
  • Thank you sir! It really is a fantastic site! Lots to learn out here. – js9 Dec 29 '15 at 06:21
  • An array and a pointer are different types! – too honest for this site Dec 29 '15 at 15:32

3 Answers3

2

First of all, when using sizeof on a pointer, it gives you the size of the datatype, i.e, the pointer itself, not the size of the allocated or pointed memory location.

So, in case of

char *str1 = "Big";

sizeof(str1) will give you the size of str1 itself, i.e, the size of char *. This will vary depending on your platform and compiler used, like for 32-bit, it will be 4 and for 64-bit, it will be 8, usually.

Then,

 char str2 = "Big";

is invalid. If you meant

 char str2[] = "Big";

in that case, str2 is an array, initialized by the supplied string and the null terminator. So, the total size will be 4 * sizeof(char) which is 4.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0
char *str1 = "Big";

str1 is a char pointer. Generally sizeof( pointer ) is fixed size, and machine-dependent. So sizeof( char* ) = sizeof( int* ) = sizeof( any pointer ) is usually 4 Bytes (32-bit machine) or 8 Bytes (64-bit machine). There can be some exceptions though.

char str2[] = "Big";  

str2 a character array with terminator '\0' at the end, thus sizeof( str2 ) = 4

Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54
  • Thank you for the answer, so is it because my system is 64 bit, giving me 8 btyes? cool! And I meant str2[], sorry! – js9 Dec 29 '15 at 05:59
  • Pointers could be any size (and pointers to different types may have different sizes on the same machine), it's best not to make any assumptions – M.M Dec 29 '15 at 06:19
  • I see, I will stop making any assumptions on that. Thank you! – js9 Dec 29 '15 at 06:20
  • @M.M - thanks for pointing it out. – artm Dec 29 '15 at 06:28
0

str1 is a pointer, Any pointer(int, char, float) size must be 4 byte or 8 byte depending on the machine.

sizeof() is used to get the actual size of any type of data in bytes.

Besides, sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.

Also, a sizeof() will give different results for the different declarations.

The second one str2, the array declaration, will return the length of the array including the terminating null character.

msc
  • 33,420
  • 29
  • 119
  • 214