Thread: whats char*?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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"?

  4. #4
    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.

  5. #5
    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.

Popular pages Recent additions subscribe to a feed