whats char*?

This is a discussion on whats char*? within the C Programming forums, part of the General Programming Boards category; Originally Posted by risby p* is not the way to dereference a pointer, *p is. * char is not the ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Quote Originally Posted by risby
    p* is not the way to dereference a pointer, *p is.

    * char is not the way to declare a pointer, char * is.
    i'm sorry i think i'm using the term declare in the wrong way. what does it mean "exactly"?

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose it depends on your point of view. However, reread what I said, and you'll see that I already qualified the statement:
    Quote Originally Posted by Quzah
    provided you've actually got the variable itself, and not just the value.
    You neglected to include that in your partial quote.

    It depends how pedantic you want to be. In short, I'm right. Because, well, I've very seldom wrong when it comes to this sort of thing. But that's an aside.

    In long...

    Everything in C is passed by value. Functions either take values contained in variables, or they take values which are the addresses of other variables (or functions). If you say you pass a "single character", you mean one of two things:

    1 - You are passing an integer value, which would fit in the allowede value for the type 'char'.
    2 - You are passing the address of the 'char', and as such, are "passing a char"(acter).

    If you have a function which wants a pointer to a character, it means one of two things:

    1 - It's pretending to be a "string" function. "strings" are zero or more values as per 1 above, terminated by a nul character.
    2 - It really wants you to pass it one char(acter), so that it can change the value in the actual char(acter) variable.

    Functions which take values (and not address[es | values]) of variables, cannot change the contents of a variable passed to them. That is to say:
    Code:
    char x;
    ...
    foo( x );
    The variable x cannot be updated by foo(). It will remain unchanged, even if foo happens to do this:
    Code:
    void foo( char bar )
    {
        bar =! bar;
    }
    x will not be changed. The function foo only gets the value of whatever was in x, not the actual variable.

    So, were we actually to use the definition "pass [a] single character[s] to a function", this wouldn't qualify. It just gets a value. Not a variable. If you want to pass a variable so that it can be modified, you need to pass it via pointer. Thus, you copy its address to the function, so the function now can access the actual variable.

    String functions take addresses of one character. (Because there's no such thing as one address representing more than one variable.) They treat that address as the start of a block of memory they're supposed to be able to play with, until they hit a value which equals the nul character.

    Thus, they take one character as an argument. They move foreward in memory from it, until they find "HEY STOP!", then they do. Or should.

    The point is, you can pass a single character to a string function. If someone wants to prove me wrong, go ahead. I need some entertainment.*


    Quzah.


    *Oh, and if you say "I meant the value of a character...", I automatically counter with: "In theory, the value 'a' could represent a spot in memory which just happens to point to an allocated block of memory my program has access to, which contains a single character. So while my compiler will warn about converting an integer to a pointer without a cast, it will be allowed, and could in fact not crash. Thus, we could in theory pass 'a' to a function, that just have the same value of an allocated 'char', containing the character 'a'."
    Hope is the first step on the road to disappointment.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Abda92
    i'm sorry i think i'm using the term declare in the wrong way. what does it mean "exactly"?
    I've written about forty posts explaining pointers, I'm sure you could find one if you wanted to. Anyway, what's one more...


    Variables hold values. Pointers are variables that hold, as their values, addresses of a specific type*.
    Code:
    char x;
    The variable x can hold the allowed ranges for type char. This is typically letters and numbers, punctuation, etc. However, you can ignore the fact that these values just happen to coincide with the ASCII (or your language setting of choice), and treat them as tiny integers. (Because that's really what they are.)

    Now, pointers are used to "point at" (hence the name) other actual variables. They also hold values, but their value is the address of the type of variable to which they're allowed to point.
    Code:
    char *p;
    The * means that p is a pointer. Read it right to left "p *(points to a) char. The value contained in p represents the address of a char, or NULL, which means it's not pointing at anything. Really, it means "This points at NOTHING."

    That's important, because "uninitialized" and "at NOTHING" are two entirely different things.

    Back on topic, to point at a variable, you have to be able to get its address. To do this, you use the & (address-of) operator.
    Code:
    char x;
    char *p;
    
    p = &x;
    This means "take the address-of x and stick it in the variable p. In other words, "make p point to x".

    To "get to" whatever something points at, we need to "dereference" the pointer. To do this, you use the * operator again.
    Code:
    char x, y;
    char *p;
    ...
    p = &x;
    ...
    y = *p;
    This means "get the value stored in the variable that p is pointing at, and place that in y."

    This is called 'dereferencing'.


    Quzah.

    *excluding void pointers. Void pointers can point at any normal type of object, and must be typecast correctly to be dereferenced. They cannot point to functions.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Abda92
    i'm sorry i think i'm using the term declare in the wrong way. what does it mean "exactly"?
    It is specifying the type of your variable. In script languages variables are usually untyped. In javascript or awk script, for example, you can just write
    Code:
    some_variable = 7.6
    but in C, Java, Pascal and the like, variables are given a specific type so that the compiler will know how much memory to allocate for them and what sort of operations can or can't be applied (so that it can warn you if you appear to be doing something silly, like Quzar :-).

    This is a declaration in C:
    Code:
    double some_variable;
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  5. #20
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Abda92
    so what is the difference between data types and an identifier? i'm sorry i really am confused
    Some C data types are:

    char, int, float, double

    An identifier is the arbitrary name you decide on for your variables:

    p, limit, i, last_character
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    ok. thank you everyone. i'v read the thread again and was able to understand almost everything. let me summerize everything then please tell me if i'm right or wrong:
    1. to declare a pointer you type:
    Code:
    int(or char) *ptr;
    2. to dereference a pointer you type:
    Code:
    foo(*ptr);
    3. a function that takes the value of a variable cannot modify it, you must pass it the address of the varible.
    So is that all right?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    is that all right?

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well the "foo( )" part is a hypothetical function call. Dereferencing just gives you whatever the value is in the type it points to. So if we had a pointer to an int, and we wanted to get the value of what it points at:
    Code:
    int x = 10;
    int y;
    int *p;
    
    p = &x; /* put the address of x in pointer p */
    y = *p; /* dereference p, which gives us the value x contains, and put that in y */
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    ok thanks everyone i really appreciate you help

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21