2

I assign the same string value to a pointer and a char array

char *str = "hello" "world";  
char str1[] = "hello" "world";

Then use sizeof()function to return their lengths

sizeof(str);     //  on my computer, it's 8 !!
sizeof(str1);    //  return 11, which is right

But both of them can be printed out right by %s:

printf("%s\n%s\n", str, str1);

So why does sizeof(str);return a wrong value ?

Bin
  • 980
  • 2
  • 12
  • 24
  • Read my [What does `sizeof(&arr)` returns?](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-returns/15177499#15177499) – Grijesh Chauhan Jul 05 '13 at 09:03
  • I answered [there](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-returns/15177499#15177499) for you, instead posting new answer here. – Grijesh Chauhan Jul 05 '13 at 10:53

5 Answers5

8

So why does sizeof(str);return a wrong value ?

It does not because sizeof returns the size in bytes of the type of its operand. From section 6.5.3.4 The sizeof operator of the C99 standard (draft n869), clause 2:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Therefore:

sizeof(str)  == sizeof(char*)
sizeof(str1) == sizeof(char[11])
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 1
    Also, since the argument "may be an expression **or** the parenthesized name of a type" (emphasis mine), `sizeof str` is clearer. – unwind Jul 05 '13 at 08:36
  • 1
    @unwind, force of habit. Don't think I have ever used `sizeof T`. – hmjd Jul 05 '13 at 08:38
  • Nice answer, thanks. In that case(given a pointer to the first character of a string), how can I iterate through the string(since I don't know the string length now)? check the end character '\0' ? – Bin Jul 05 '13 at 08:38
  • @zbjosh, yes just check for null terminator. – hmjd Jul 05 '13 at 08:39
1

The first one is a pointer. You don't count the number of chars but the size of the pointer in memory. You need to understand the difference. char* point to a position in memory that you assume contains chars in a row. char[x] IS a position in memory which has x chars on a row there.

Avi
  • 21,182
  • 26
  • 82
  • 121
1

The value 8 is the size of the pointer as str is actually a pointer just as it looks and i.e. you can reassign it to point to another string literal.

The variable str1 is an array of characters and as such cannot be reassigned and its size is related to the size of the array.

jszpilewski
  • 1,632
  • 1
  • 21
  • 19
0

First one is pointer to char, other one is array. Pointer points to one char only, size is one byte.

You can use char* for array, but you need to know count in separate variable.

EnterSB
  • 984
  • 2
  • 10
  • 27
0

str* is of type pointer, str[] is of type char[11]. You therefore the sizeof() will return different values, it returns the size of the type and not the length of the string.

You should use strlen() to check the sizeof a string. Keep in mind that that every string you test with strlen() must end with a terminating null (\0).

For constants (define in the code using "lorem ipsum") the terminating null is added by the compiler.

The only support for strings in the C programming language itself is that the compiler will translate a quoted string constant into a null-terminated string, which is stored in static memory.

https://en.wikipedia.org/wiki/C_string_handling

Aloys
  • 682
  • 4
  • 11