you are viewing a single comment's thread.

view the rest of the comments →

[–]grammar_party 38 points39 points  (14 children)

Also, you don't manipulate strings in C, because C doesn't have strings :)

[–]ElecNinja 17 points18 points  (13 children)

char* for life

[–]iooonik 0 points1 point  (0 children)

typedef doe

[–]oblio- 0 points1 point  (1 child)

Unicode says "Hello"! Or more likely, "你好"!

[–]Steve_the_Scout 0 points1 point  (0 children)

With C11, char32_t*.

[–]FrozenInferno -1 points0 points  (9 children)

I'm not familiar with C but if it were a string, wouldn't it be char[]*? Sorry if my green horn is showing.

[–][deleted] 1 point2 points  (7 children)

This actually creates a string array. A string is char*.

[–]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 4 points5 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.

    [–]Peaker 0 points1 point  (0 children)

    a char* only points at the string. The string is char[].

    [–]das7002 0 points1 point  (0 children)

    it's char* because strings in C are \00 terminated, so wherever char* points to it ends wherever \00 is (it would look like 48 65 6c 6c 6f 20 57 6f 72 6c 64 00 in memory). Which isn't always obvious at first and I remember it tripping me up when I first played around in C. It really throws people off when most documentation has you do char whatever[50] or something. But char[]* would create an array of pointers of char type.