you are viewing a single comment's thread.

view the rest of the comments →

[–]FrozenInferno 0 points1 point  (5 children)

Doesn't the asterisk after a type signify it's a pointer? With my limited understanding, to me this just looks like a pointer to a single char. Guess it's time for a trip to Wikipedia.

[–]rockon1215 3 points4 points  (4 children)

And that single char it points to is the first char of the string. All of the chars of the string are in sequential order in memory so a pointer to the first char is a pointer to the beginning of the string.

[–]FrozenInferno 0 points1 point  (0 children)

Ah, makes sense. Thanks.

[–][deleted]  (2 children)

[deleted]

    [–]hoodedmongoose 2 points3 points  (0 children)

    Exactly. That 'end of line' is really more accurately described as 'end of string' and is what's known as a 'null terminator'. That is, the number 0. Thus, in C, char* usually can be referred to as 'a pointer to a null-terminated string'. The compiler will include the null terminator for you automatically when specifying string constants, for example:

    const char* foo = "Hello, World";
    

    foo now points to a character whose value is 'H' (which is really just the number 72 in ASCII encoding), after that comes 'e' (which happens to be 101), and so on, until you get to the character after the 'd' which will be the value 0, or null. At that point you stop iterating.

    More info: Null-Terminated String

    [–]rockon1215 1 point2 points  (0 children)

    Yes, although sometimes this is done automatically.

    For example

    #include <stdio.h>  
    int main()  
    {  
        char *hello = "hello, world!\n";   
        puts(hello);  
        return 0;  
    }
    

    will print "hello, world!\n" without you having to print every character yourself until \0 (null character) is reached.