0

I tried to calculate the length of a character array in the following ways:

char *s="abcde";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

n comes out to be constant value 4, no matter how long the string is. Whereas had i declared the array as

char s[]="abc";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

The output still remains 4. I understand that in the second case, it includes the concluding character '\0', hence the output. The only thing i didn't understand is why i get a constant output in first case.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user3250183
  • 506
  • 6
  • 19
  • 2
    you need to use strlen() to get length of the string. – tp1 Feb 01 '14 at 12:13
  • 2
    Read [Difference between `char *str` and `char str[]` and how both are stored in memory](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Feb 01 '14 at 12:18

4 Answers4

3

With char *s you make a pointer s that points to some other memory. With char s[] = ... you make a an array of N characters.

Maybe it's easier if you look at it like this:

For the pointer version, char *s = "abcde", it will be something like

+---+      +-----------+
| s | ---> | "abcde\n" |
+---+      +-----------+

While for the case with the array, char s[] = "abc" it will be like

+---------+
| "abc\0" |
+---------+

This should make it easy to see why you can't use sizeof on the pointer, as it returns the size of the pointer and not what it points to. I have also added the string terminator that exists for all string literals, and this is why you get the size 4 for the array, it actually is four characters.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

char *s is a pointer to either a char, or a sequence of char. On a 32bit architecture it will be 4 bytes wide, so sizeof(s) will be 4. A single character is (usually) 1 byte, so sizeof(s[0]) will be 1. Therefore, n will be 0.

When you use a char[] type the compiler treats it a a fixed length sequence, it's just working out how long the sequence will be for you, in your case it's 4 characters long. However, if you had:

char s[]="Hello, world";
int n=sizeof(s)/sizeof(s[0]);

Then n would be 13, as there the 12 character you entered, plus the null terminator at the end.

Sean
  • 60,939
  • 11
  • 97
  • 136
2

In this code snippet

char *s="abcde";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

s is a pointer. So sizeof( s ) is equal to the size of pointers in the system. In your system the size of a pointer is equal to 4. As the type of s[0] is char then its size equal to 1 and you get value 4.

In the second code snippet

char s[]="abc";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

s is an array. Its size is determined by the size of the initializer. As string literal "abc" has size equal to 4 because the terminating zero is also counted then the size of array s is 4. If you for example would write

char s[]="abcde";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

then the size of the string literal is equal to 6 and correspondingly the size of the array will be also equal to 6.

You could the same code rewrite the following way

char s[6]="abcde";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

If you write this code as

char s[10]="abcde";
int n=sizeof(s)/sizeof(s[0]);
cout<<n;

then the size of the array will be equal to 10 though the size of the string literal is equal to 6. All other elements of the array that have no initializer will be zero-initialized. That is the array would look as

[a][b][c][d][e]['\0']['\0']['\0']['\0']['\0']
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In the first case, s is an object of type char*. sizeof(s) evaluates to the size of this "pointer to char" object, which is 4 (in your execution environment), not the length of the string that s points to (which strlen(s) evaluates to).

neverhoodboy
  • 1,040
  • 7
  • 13