0

A pretty basic C question. Why do the following two commands result in the creation of strings that have different sizes?

As you can see below, method 1 creates a string with size 8 bytes and method 2 creates a string with size 5 bytes.

Am confused as to why method 1 is creating a string of size 8 bytes...

(I've already seen these posts: Difference of sizeof between char* x and char x[] and What is the difference between char s[] and char *s?. Unless I had a reading-comprehension-fail, it doesn't really address why method 1 creates a string of size 8 bytes... According to the responses, it would seem that method 1 should be creating a pointer of size 4 bytes.)

Method 1:

char *string = "ABCD";

Method 2:

char string2[5] = "ABCD";

For example, when I run the following program, I get the output shown below.

#include <stdio.h>

int main(int argc, char *argv[])
{
  char *string = "ABCD";

  printf("Based on \"char *string = \"ABCD\":\n");
  printf("Size of string: %ld\n",sizeof(string));
  printf("Size of each element of string: %ld\n",sizeof(string[0]));
  printf("String: %s\n\n", string);

  char string2[5] = "ABCD\0";

  printf("Based on \"char string2[5] = \"ABCD\":\n");
  printf("Size of string: %ld\n",sizeof(string2));
  printf("Size of each element of string: %ld\n",sizeof(string2[0]));
  printf("String: %s\n\n", string2);

  return 0;
}

Output of the above program:

enter image description here

Community
  • 1
  • 1
iceman
  • 2,020
  • 2
  • 17
  • 24
  • 2
    possible duplicate of [What is the difference between char s\[\] and char \*s in C?](http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c) – M.M Oct 08 '14 at 22:54
  • another possible dupe: http://stackoverflow.com/questions/15538210/sizeof-a-pointer – M.M Oct 08 '14 at 23:32
  • Also, for `char string3[] = "ABCDE";` (no size declared) `sizeof(string3) == 6` (5 letters + the nul) – Stephen P Oct 08 '14 at 23:53

3 Answers3

1

that's because in this line char *string = "ABCD"; you are declaring a pointer to char which a size of 8 bytes where as in this line char string2[5] = "ABCD"; you are declaring an array of 5 chars and since a char is 1 byte long and 1 * 5 == 5 bytes then the size of string2 will be 5 bytes

Farouq Jouti
  • 1,657
  • 9
  • 15
  • This may be an inane question but can you elaborate as to why a pointer to char has a size that's 8 times larger than the type it's pointing to (or 2x larger if you consider the four characters in ABCD, i.e., excluding /0)? (I understand why `char string2[5] = "ABCD"` results has a size of 5 bytes. ) – iceman Oct 08 '14 at 23:00
  • 1
    because a pointer stores memory address not characters and memory addresses are usually 8 bytes – Farouq Jouti Oct 08 '14 at 23:03
  • I understand now, thanks. I think I was confused because some of the responses in the other threads implied that pointers took up only 4 bytes of memory, which is indeed the case in 32-bit but not for 64-bit (and I'm obv running the latter). – iceman Oct 08 '14 at 23:09
0

A char only has to hold an ASCII value, which is a very small range of numbers, so they only take up 1 byte each.

A char* is a pointer to an address in memory, which is a much larger range of numbers, hence more bytes are required to store it. A pointer on a 32-bit system takes up 4 bytes, and on a 64-bit system, takes up 8 bytes.

I take it you're running on a 64-bit system? I think that's why it's 8 bytes.

0

In your first example you declare a variable that is a pointer to a char. So, when you take its size with sizeof, you get the size of the pointer (not the size of the thing it's pointing at), which is 8 bytes on a 64-bit platform.

In the second example, you declare a variable that is an array of char. In C, the sizeof an array does as you would expect - it returns the size of the array in bytes, which in this case is 5 * sizeof(char) (since you declared a 5 character array), which is 5 (since sizeof(char) = 1).

In either case, using sizeof is generally not the correct way to measure the length of a string - you should be using strlen instead. This will correctly handle cases where the string terminator character NUL appears somewhere other than at the end of the char array.

Mac
  • 14,615
  • 9
  • 62
  • 80