0

I just started learning C after I already got a few years experience in Python, C# and Java.

I learned in a tutorial, that char anything[] is always a pointer. (Today, someone told me this is wrong) - I think my question has something to do with this. Nevertheless, I'm trying to get the length of a char-array:

#include <stdio.h>

int get_string_length(char * string)
{
    int length = 0;
    while(string[length] != '\0')
    {   
        char c = string[length];
        length++;
    }
    return length;
}

int get_string_size(char * string)
{
    return sizeof(string);
}

int main()
{
    printf("%d\n", get_string_size("hello world")); // returns 8
    printf("%d\n", get_string_length("hello world")); // returns 11
    printf("%d\n", sizeof("hello world")); // returns 12 -> Okay, because of '\0'-char
    return 0;
}

result:

8

11

12

so, why is my get_string_size-method returning 8 instead of 12? (Since both only call sizeof())

Community
  • 1
  • 1
Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
  • 2
    `get_string_size(char*)` returns 8 because that's the size in bytes of a pointer on your machine, you can use `strlen(char*)` to get what I guess is your expected result. – George Feb 06 '17 at 13:33
  • So many answeres in that short time - and also a duplicate ... I think I'm really at starting point of C... :D thanks for all answeres... I think I need to read a bit more – Matthias Burger Feb 06 '17 at 13:37

3 Answers3

5

char anything[] is a pointer? Not quite.

The actual type of the literal "hello world" is a const char[12]. (Note the extra element for the NUL-terminator).

But when passed to a function, this type decays to a const char*.

So get_string_size returns sizeof(const char*) which is 8 on your platform (i.e. the sizeof a char*), but sizeof("hello world") is sizeof(const char[12]) which is 12, since sizeof (char) is defined by the C standard to be 1.

get_string_length is returning the position of the first NUL terminator starting from the pointer passed to it.

Finally, note that you should use %zu as the format specifier for the sizeof return type: technically the behaviour of printf("%d\n", sizeof("hello world")); is undefined.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Always use strlen() to determine the length of a string.

Using sizeof() for string length is dangerous and should be avoided. sizeof() returns the size of the given type, and this case the given type is a pointer (your system must be 64-bit since the size is 8).

Consider this code and its output:

char string[] = "This is a test";
char *pointer = string;

printf("sizeof string: %zu\n", sizeof(string));
printf("sizeof pointer: %zu\n", sizeof(pointer));
printf("strlen pointer: %zu\n", strlen(pointer));

sizeof string: 15
sizeof pointer: 8
strlen pointer: 14
1

This does not do what you think:

int get_string_size(char * string)
{
    return sizeof(string);
}

Here sizeof(string) is the size of the pointer, which is always 8 on your (presumably 64-bit) platform. On a 32-bit platform it would be most likely 4 instead of 8.

On the other hand sizeof("hello world") is the memory size taken by the string literal "hello world", which is 11 + 1 (+1 for the NUL terminator).

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115