-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?

5
  • 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. Commented Dec 29, 2015 at 6: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. Commented Dec 29, 2015 at 6: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. Commented Dec 29, 2015 at 6:19
  • Thank you sir! It really is a fantastic site! Lots to learn out here. Commented Dec 29, 2015 at 6:21
  • An array and a pointer are different types! Commented Dec 29, 2015 at 15:32

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

oh, yes I meant str2[]. I see! So the size of a pointer is 8 bytes? Thank you!
But be aware, the sizeof (a pointer) is not always 8. For instance on x86 the size is 4. So it is dependent on the architecture.
Okay, thank you very much!
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.

1 Comment

Pointers could be any size, it's best not to make any assumptions
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

4 Comments

Thank you for the answer, so is it because my system is 64 bit, giving me 8 btyes? cool! And I meant str2[], sorry!
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
I see, I will stop making any assumptions on that. Thank you!
@M.M - thanks for pointing it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.