Thread: whats char*?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    whats char*?

    i'v been reading the tutorials on this website until i got to lesson 9: C Strings. it mentions that single characters cannot be passed into functions. the function is expecting char*. what is that? i'v read all the tutorials before it and am pretty sure that none of them mention anything about char*. i know from the pointers tutorial that *char is used to declare pointers, and to access the actual memory address. so what does char* do?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your pointer tutorial, or at least your understanding of it, is backwards. There is no '* char'. It's 'char *'. Read it right to left. "Pointer to a char".


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I find a lot of new programmers forget it's char* not *char because they're so used to typing
    Code:
    char *myVar;
    
    /* not */
    
    char* myVar;
    That's just what I've observed, though.
    Sent from my iPadŽ

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    I find a lot of new programmers forget it's char* not *char because they're so used to typing
    Code:
    char *myVar;
    
    /* not */
    
    char* myVar;
    That's just what I've observed, though.
    Whitespace doesn't matter. You can even do:
    Code:
    char*foo;
    The * ties to the variable, not the type, so this:
    Code:
    char* foo, bar;
    only gives you 1 pointer and 1 char. It doesn't make 'foo' and 'bar' pointers. For that you'd need something like:
    Code:
    char *foo, *bar;
    Again, white space doesn't matter. It could have been:
    Code:
    char*foo,*bar;
    char* foo, *bar;
    char *foo,* bar;
    char * foo, * bar;

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I think you're missing my point. I know how the operator works... what I'm saying is new programmers are so used to typing it directly preceding a word rather than following a word, that when they simply write the data type char* without an identifier, they accidentally write it *char. You see?

    There were several people in the lower level programming courses that I took who would write a pointer declaration fine, but when it came to prototyping, they'd often have
    Code:
    void myFunc(const *char);
    ...or something of that nature. Again, just my observation... I don't know if that's the real reason it's done as often as it is.
    Last edited by SlyMaelstrom; 09-12-2006 at 01:31 AM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    ok so for declairing a pointer i type
    Code:
    int *pointer
    for example and to access the actual memory address i use
    Code:
    printf("%d", pointer*);
    is that right?

  7. #7
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Abda92
    i'v been reading the tutorials on this website until i got to lesson 9: C Strings. it mentions that single characters cannot be passed into functions. the function is expecting char*. what is that? i'v read all the tutorials before it and am pretty sure that none of them mention anything about char*. i know from the pointers tutorial that *char is used to declare pointers, and to access the actual memory address. so what does char* do?
    Hmmm, while getting twisted knickers about your typo nobody has addressed your point so far, have they? I think the wording of lesson 9 has misled you, you certainly are able to pass a single character to a function and if the function declaration specifies a char data type rather than a char * data type it will be expecting you to pass a single character. Mistakes are made when people think that "a" is a single character. "a" in C is actually a string, comprising a byte with the value of the character 'a' followed by a byte with the value zero. If you want to pass the single character you must pass either the character literal 'a' or a char data type variable.

    Quote Originally Posted by lesson 9
    Remember how arrays act like pointers when passed into functions? Characters don't, so if you pass a single character into a function, it won't work; the function is expecting a char*, not a char.
    This should say "so if you pass a single character into a function which is expecting a string, it won't work".
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Abda92
    ok so for declairing a pointer i type
    Code:
    int *pointer
    for example and to access the actual memory address i use
    Code:
    printf("%d", pointer*);
    is that right?
    Abda92 == "100% Confused"

    No, you would printf("%d", *pointer);

    Please notice the difference between a data type and an identifier.
    Sent from my iPadŽ

  9. #9
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Abda92
    ok so for declairing a pointer i type
    Code:
    int *pointer
    for example and to access the actual memory address i use
    Code:
    printf("%d", pointer*);
    is that right?
    No, it's

    Code:
    int teajerr = 9;
    int * p = &teajerr;
    
    print ("%d\n", *p);
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by risby
    Hmmm, while getting twisted knickers about your typo nobody has addressed your point so far, have they?
    Quote Originally Posted by risby
    If you want to pass the single character you must pass either the character literal 'a' or a char data type variable.
    Or the address of said variable, ie, a pointer to a character.
    Code:
    void foo( char * bar );
    ...
    
    char bar;
    ...
    foo( &bar );

    Quote Originally Posted by risby
    This should say "so if you pass a single character into a function which is expecting a string, it won't work".
    Sure it can.
    Code:
    char foo = '\0';
    size_t x;
    ...
    x = strlen( &foo );
    That'll work.

    See, if you're going to try to correct people, be correct.

    You can pass single characters to functions which take pointers to characters, provided you've actually got the variable itself, and not just the value. If it "works" or not, depends on the value of the variable, and the function. It's usually not a good idea to pass single characters to all of the "string" functions. But it can be done.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by quzah
    See, if you're going to try to correct people, be correct.

    You can pass single characters to functions which take pointers to characters,
    I don't think your comment helps Quzah. You are not passing "single characters to a function which takes pointers to character" if you pass the address of a single character; you are passing a pointer.
    Last edited by risby; 09-12-2006 at 02:24 AM.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    ok i took this piece of code from the tutorial and compiled it, and it worked! but it doesn't use p*, it uses*p
    Code:
    #include <stdio.h>
    
    int main()
    { 
        int x;            /* A normal integer*/
        int *p;           /* A pointer to an integer ("*p" is an integer, so p
                           must be a pointer to an integer) */
    
        p = &x;           /* Read it, "assign the address of x to p" */
        scanf( "%d", &x );          /* Put a value in x, we could also use p here */
        printf( "%d\n", *p ); /* Note the use of the * to get the value */
        getchar();
    }
    EDIT: sorry, the page didn't refresh until i postid this, ignore it
    Last edited by Abda92; 09-12-2006 at 02:30 AM.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Abda... a little bit of reading into what has been said in this post would have solved your questions hours ago.
    Sent from my iPadŽ

  14. #14
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Abda92
    i know from the pointers tutorial that *char is used to declare pointers
    Quote Originally Posted by Abda92
    ok i took this piece of code from the tutorial and compiled it, and it worked! but it doesn't use p*, it uses*p
    p* is not the way to dereference a pointer, *p is.

    * char is not the way to declare a pointer, char * is.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    so what is the difference between data types and an identifier? i'm sorry i really am confused
    Last edited by Abda92; 09-12-2006 at 06:15 AM.

Popular pages Recent additions subscribe to a feed