Thread: a quick question about pointers

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    a quick question about pointers

    after my question yesterday about passing a 2d string array to a function i thought i had better go back and review the chapter on pointers.
    Code:
    int main()
    {
        int x, y;
        int *p_x = &x, *p_y = &y; // declare pointers to x and y
    
        x = 5;
        printf("x = %d\n", x);
        printf("p_x = %X\n", *p_x); 
    
        *p_x = 9; // makes x = 9
        printf("x = %d\n", x);
        printf("p_x = %X\n", *p_x);
    
        y = 3;
        *p_x = *p_y; // makes x the value of y
        y = 6;
    
        printf("p_x = %X\n", *p_x);
        p_x = p_y;      //assigns the address of y to the pointer p_x
        printf("p_x = %X\n", *p_x);
        
        return 0;
    the comments are what i got when the program ran not what i expected to get.

    question is if i have to designate a pointer by putting an astrix in front of the variables name ie *p_x rather than p_x how come when you use the point like in the above examples its the opposite.

    many thanks
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    Read Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right? That FAQ is about style (in C++, but comparing with C), but reading the motivations behind the style in terms of syntax and type will help you understand what this is about.

    So, you will see that p_x is a pointer (i.e., its type is pointer to int), so *p_x is an int, and then when declaring it we effectively declare that *p_x is an int; and in so doing declare that p_x is an int*
    Last edited by laserlight; 04-28-2019 at 02:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    "point like"?

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    I don't understand what you're asking.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i was asking that if i have to declare a point by using the astrix ie int *p_x a pointer to x. how come *p_x is the value of x and not the memory address. "*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be. so if i wanted to print the value of x using the pointer i would of thought printf("%d", p_x) but that appears to give the memory address. if i want to print x's value with the pointer i have to give it the full *p_x which was the variable we stored the address in.... and round we go lol

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    Quote Originally Posted by cooper1200 View Post
    how come *p_x is the value of x and not the memory address.
    I get what you're asking now. The asterisk is thrown around in the syntax for pointers quite liberally and means different things in different places. In the variable declaration as part of the declarator (the things like * or [] along with the identifier), the * means "this is a pointer." A pointer variable itself is just an unsigned integer type (though may be larger than an unsigned int) that holds the address of what you're pointing to which is why you have to use the & address of operator. The value of that variable is the address. In order to follow that address, you have to use the unary * operator, the dereference operator. On the left-hand side of an assignment operator, this means "store the value from the expression on the right side in this memory location" and in all other places it means "read the value from this memory location."

    That's the what, what about the why? I think the point was to always make you aware that you're dealing with a pointer. If you have int a, b, c, d, *e, f, g, it's easy to forget that e is a pointer. If e=10 somehow changes f because e pointed to f, it can be very difficult to track down a bug. But always having to say *e = 10 is a constant reminder that e points to something, that it changes something else. They could have made things transparent by using the opposite syntax and doing away with the -> operator, using the . operator everywhere, but I think that would have been a detriment to the language.


    Quote Originally Posted by cooper1200 View Post
    "*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be. so if i wanted to print the value of x using the pointer i would of thought printf("%d", p_x) but that appears to give the memory address. if i want to print x's value with the pointer i have to give it the full *p_x which was the variable we stored the address in.... and round we go lol
    Here's where I think your confusion might be. In this case, *p_x = &x is, I'm assuming, part of a declaration. In that case, * is part of the declarator, it's a different syntax than the * unary dereference operator. I didn't even realize this was inconsistent until you pointed it out, it's not something you'll notice as you continue to use C. If it really bothers you, declare and assign to pointers in different statements.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    if its the way it is its the way it is. just so i have this right in my thick skull. *p_x + 1 increments the value of x by one?????

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    Well... no. *p_x + 1 is an expression that evaluates to the value of x + 1, assuming p_x points to x. *p_x++ increments the value of x, or *p_x += 1.

    Just remember that in declarations *p_x = 0 assigns the value 0 to p_x, meaning the address stored in p_x will be 0. But in all other statements, *p_x = 0 assigns 0 to whatever p_x points to. You'll get this with practice, you just need to repeat the pattern a few times.

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    9
    Quote Originally Posted by cooper1200 View Post
    "*p_x" is the declared variable
    p_x is the declared variable, of type int *. A pointer to an int. You can see it like this (int *)p_x. In the declaration int *p_x = &x its the same if you do it in diferent statements: int *p_x; p_x = &x, cause the declared variable is p_x without the *. After the declaration, * becomes the indirection operator that you will want to use for access and store/retrieve values to/from the place where p_x(the declared variable) is pointing

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    Quote Originally Posted by gaxio
    *p_x++ increments the value of x, or *p_x += 1.
    *p_x++ is equivalent to *(p_x++), i.e., it post-increments the pointer, not what the pointer points to.

    EDIT:
    Quote Originally Posted by cooper1200
    "*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be.
    *p_x is the declared variable of type int according to the grammar. Semantically though, p_x is the declared variable of type int* (i.e., pointer to int; recall that * cannot be part of a variable name, so *p_x isn't really a "variable"). In an initialisation (i.e., it is part of a declaration, in particular the declaration that is the definition of the variable), you have the two worlds of declaration and expression colliding: so now even though according to the grammar it looks like you're declaring *p_x, because you want to initialise it, the semantics matters: you're initialising p_x, not *p_x, hence it is correct to use &x, since that results in a pointer to x.
    Last edited by laserlight; 04-28-2019 at 04:45 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    Quote Originally Posted by laserlight View Post
    *p_x++ is equivalent to *(p_x++), i.e., it post-increments the pointer, not what the pointer points to.
    Oops, I certainly shouldn't be making mistakes like that when trying to explain things

    This is a very common mistake, though. They've tried to eliminate in more modern languages. In Go, you can't even use ++ in expressions, and Rust doesn't even have ++ and -- operators.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by gaxio View Post
    Oops, I certainly shouldn't be making mistakes like that when trying to explain things

    This is a very common mistake, though. They've tried to eliminate in more modern languages. In Go, you can't even use ++ in expressions, and Rust doesn't even have ++ and -- operators.
    After a while everyone makes mistakes responding to questions - As long as you admit that it was a mistake and not double down no one really cares.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about pointers!
    By chickenlittle in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2011, 11:35 PM
  2. hello all quick pointers question
    By dogger in forum C Programming
    Replies: 11
    Last Post: 05-22-2011, 09:03 AM
  3. Quick question on pointers
    By newbie30 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 03:46 AM
  4. A quick question about pointers.
    By waynex in forum C Programming
    Replies: 6
    Last Post: 03-04-2008, 07:12 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

Tags for this Thread