dereference

Discussion in 'C Programming' started by Bill Cunningham, Aug 20, 2012.

  1. I believe I have derefencing down now. If I am understanding right it
    has all to do with pointers. The dereference in this code worked but for
    some reason I am getting some big negative value for x before it's
    dereferenced. What's wrong with the pointer here?

    Bill

    #include <stdio.h>

    int main(void)
    {
    int x;
    x = 2;
    int *px = &x;
    printf("%d\n", px);
    *px = 5;
    printf("%d\n", x);
    return 0;
    }
    Bill Cunningham, Aug 20, 2012
    #1
    1. Advertising

  2. "Bill Cunningham" <> writes:
    > I believe I have derefencing down now. If I am understanding right it
    > has all to do with pointers. The dereference in this code worked but for
    > some reason I am getting some big negative value for x before it's
    > dereferenced. What's wrong with the pointer here?


    No, you clearly don't have dereferencing down now.

    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int x;
    > x = 2;
    > int *px = &x;
    > printf("%d\n", px);


    The problem here is that you're not dereferencing px. Change `px` to
    `*px` and it will work.

    Did your compiler not warn you about the type mismatch (passing an int*
    to printf when it requires an int argument?

    > *px = 5;
    > printf("%d\n", x);
    > return 0;
    > }


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 20, 2012
    #2
    1. Advertising

  3. Keith Thompson wrote:
    > "Bill Cunningham" <> writes:
    >> I believe I have derefencing down now. If I am understanding
    >> right it has all to do with pointers. The dereference in this code
    >> worked but for some reason I am getting some big negative value for
    >> x before it's dereferenced. What's wrong with the pointer here?

    >
    > No, you clearly don't have dereferencing down now.
    >
    >> #include <stdio.h>
    >>
    >> int main(void)
    >> {
    >> int x;
    >> x = 2;
    >> int *px = &x;
    >> printf("%d\n", px);

    >
    > The problem here is that you're not dereferencing px. Change `px` to
    > `*px` and it will work.


    Oh I thought just entering the value px would print 2 in that first
    printf.


    > Did your compiler not warn you about the type mismatch (passing an
    > int* to printf when it requires an int argument?


    No but so many people have told me use the %d for decimal. I must be
    getting confused. I used to use %i all the time. My little mistake.

    >> *px = 5;
    >> printf("%d\n", x);
    >> return 0;
    >> }
    Bill Cunningham, Aug 20, 2012
    #3
  4. Bill Cunningham

    Les Cargill Guest

    Bill Cunningham wrote:
    > I believe I have derefencing down now. If I am understanding right it
    > has all to do with pointers. The dereference in this code worked but for
    > some reason I am getting some big negative value for x before it's
    > dereferenced. What's wrong with the pointer here?
    >
    > Bill
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int x;
    > x = 2;
    > int *px = &x;

    // this printf prints the value of the pointer,
    // not value of the thing the pointer points to.
    > printf("%d\n", px);
    > *px = 5;

    // this printf prints the value of the thing the pointer
    // points to.
    > printf("%d\n", x);
    > return 0;
    > }
    >
    >


    On my setup:

    C:\c\usenet>gcc px.c

    C:\c\usenet>a
    2686756
    5

    C:\c\usenet>

    2686756 is the value of px ( 0x28ff24 ). You won't get exactly that value.
    5 is the value of both *px and x

    I don't see any problem.

    --
    Les Cargill
    Les Cargill, Aug 20, 2012
    #4
  5. Les Cargill wrote:

    >> #include <stdio.h>
    >>
    >> int main(void)
    >> {
    >> int x;
    >> x = 2;
    >> int *px = &x;

    > // this printf prints the value of the pointer,
    > // not value of the thing the pointer points to.


    Oh and that's what I wanted.

    >> printf("%d\n", px);
    >> *px = 5;

    > // this printf prints the value of the thing the pointer
    > // points to.
    >> printf("%d\n", x);
    >> return 0;
    >> }


    [snip]
    Bill Cunningham, Aug 20, 2012
    #5
  6. Keith Thompson wrote:

    > No, you clearly don't have dereferencing down now.
    >
    >> #include <stdio.h>
    >>
    >> int main(void)
    >> {
    >> int x;
    >> x = 2;
    >> int *px = &x;
    >> printf("%d\n", px);

    >
    > The problem here is that you're not dereferencing px. Change `px` to
    > `*px` and it will work.


    So when printing a pointer it should always be derefenced then. I missed
    that. What I've been reading about dereferencing this that the pointer
    changes the value of the pointee.

    Bill
    Bill Cunningham, Aug 21, 2012
    #6
  7. "Bill Cunningham" <> writes:
    > Keith Thompson wrote:
    >> "Bill Cunningham" <> writes:
    >>> I believe I have derefencing down now. If I am understanding
    >>> right it has all to do with pointers. The dereference in this code
    >>> worked but for some reason I am getting some big negative value for
    >>> x before it's dereferenced. What's wrong with the pointer here?

    >>
    >> No, you clearly don't have dereferencing down now.
    >>
    >>> #include <stdio.h>
    >>>
    >>> int main(void)
    >>> {
    >>> int x;
    >>> x = 2;
    >>> int *px = &x;
    >>> printf("%d\n", px);

    >>
    >> The problem here is that you're not dereferencing px. Change `px` to
    >> `*px` and it will work.

    >
    > Oh I thought just entering the value px would print 2 in that first
    > printf.


    You thought wrong, and I just told you how to fix it.

    >> Did your compiler not warn you about the type mismatch (passing an
    >> int* to printf when it requires an int argument?

    >
    > No but so many people have told me use the %d for decimal. I must be
    > getting confused. I used to use %i all the time. My little mistake.


    This has nothing to do with %d vs. %i. If you apply the fix I
    suggested, %d will work just fine.

    >>> *px = 5;
    >>> printf("%d\n", x);
    >>> return 0;
    >>> }


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 21, 2012
    #7
  8. "Bill Cunningham" <> writes:
    > Keith Thompson wrote:
    >> No, you clearly don't have dereferencing down now.
    >>
    >>> #include <stdio.h>
    >>>
    >>> int main(void)
    >>> {
    >>> int x;
    >>> x = 2;
    >>> int *px = &x;
    >>> printf("%d\n", px);

    >>
    >> The problem here is that you're not dereferencing px. Change `px` to
    >> `*px` and it will work.

    >
    > So when printing a pointer it should always be derefenced then. I missed
    > that. What I've been reading about dereferencing this that the pointer
    > changes the value of the pointee.


    To "dereference" a pointer is to access the object that it points to.

    If you want to print a pointer value, use "%p" and cast the pointer
    value to void*. (That's probably not what you want to do here.)

    If you want to print the value of the object that the pointer points to,
    you need to dereference it, using the unary "*" operator.

    This would have been less annoying if you hadn't claimed to understand
    dereferencing.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 21, 2012
    #8
  9. Bill Cunningham

    Mark Bluemel Guest

    On 21/08/2012 00:43, christian.bau wrote:
    > On Aug 20, 11:04 pm, "Bill Cunningham" <> wrote:
    >> I believe I have derefencing down now. If I am understanding right it
    >> has all to do with pointers. The dereference in this code worked but for
    >> some reason I am getting some big negative value for x before it's
    >> dereferenced. What's wrong with the pointer here?

    >
    > Nothing wrong with the pointer. The problem is with the person who
    > wrote the code.
    >

    Indeed. Trip trap, trip trap went the goats over the bridge...
    Mark Bluemel, Aug 21, 2012
    #9
  10. christian.bau wrote:

    > Nothing wrong with the pointer. The problem is with the person who
    > wrote the code.


    Yeah I guess you are 100% right.

    Bill
    Bill Cunningham, Aug 21, 2012
    #10
  11. Keith Thompson wrote:

    [snip]

    > This would have been less annoying if you hadn't claimed to understand
    > dereferencing.


    From what I've been reading I thought I did. I guess not. Not fully
    obviously.

    Bill
    Bill Cunningham, Aug 21, 2012
    #11
  12. Keith Thompson wrote:

    > To "dereference" a pointer is to access the object that it points to.


    OK

    > If you want to print a pointer value, use "%p" and cast the pointer
    > value to void*. (That's probably not what you want to do here.)


    ...Ok

    > If you want to print the value of the object that the pointer points
    > to, you need to dereference it, using the unary "*" operator.


    I am taking a lot less meds now and can think much more clearly.
    Clonazepam really confuses you. I hope it is showing. I am determined to
    learn C. I am reading code and trying to learn from it.

    B
    Bill Cunningham, Aug 21, 2012
    #12
  13. Kenneth Brody <> writes:
    > On 8/20/2012 6:37 PM, Bill Cunningham wrote:

    [...]
    > int *px = &x;
    >
    > While it is not out of the realm of possibility that "&x" is also 2, it
    > would be by pure coincidence, not because of the "x=2" assignment.

    [...]

    Yes, it *is* out of the realm of possibility that &x is 2, since &x is
    of type int* and 2 is of type int. There isn't even an implicit
    conversion between those types; `&x == 2` is a constraint violation.

    It's possible that `&x == (int*)2`, but that's a quite different thing.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 22, 2012
    #13
  14. Kenneth Brody wrote:

    [snip]

    > No. When printing what a pointer points to, dereference it. When
    > printing a pointer itself, cast it to "(void *)" and use the "%p"
    > format specifier.
    > However, based on your description, you want to be doing the former.


    Thanks. I will remeber these things better now. I might not clearly
    understand them but someday I hopefully will. But I will do as you say.

    Sincerely,

    B
    Bill Cunningham, Aug 22, 2012
    #14
  15. On Wed, 22 Aug 2012 18:10:46 -0400, "Bill Cunningham"
    <> wrote:

    >Kenneth Brody wrote:
    >
    >[snip]
    >
    >> No. When printing what a pointer points to, dereference it. When
    >> printing a pointer itself, cast it to "(void *)" and use the "%p"
    >> format specifier.
    >> However, based on your description, you want to be doing the former.

    >
    > Thanks. I will remeber these things better now. I might not clearly
    >understand them but someday I hopefully will. But I will do as you say.


    If you do what is said here without understanding why, then you are no
    better off than when you started any of your posts, including this
    one.

    You repeatedly claim you want to learn C. You can't do that if you
    bypass the understanding part.

    --
    Remove del for email
    Barry Schwarz, Aug 23, 2012
    #15
  16. Barry Schwarz wrote:

    > If you do what is said here without understanding why, then you are no
    > better off than when you started any of your posts, including this
    > one.
    >
    > You repeatedly claim you want to learn C. You can't do that if you
    > bypass the understanding part.


    I mean the deeper aspects of C. Some things at first you have to take on
    faith before it finally clicks for good. For example,

    printf("%i\n",*px); is the same as
    printf("%p\n",(void*)px);
    I don't know why and can't get into a complicated discussion about
    why...yet. But I will remember this. When I say the deeper aspects of C I am
    thinking specifically of types in parenthesis. I don't need to get into that
    yet.

    (int) function (...);

    Bill
    Bill Cunningham, Aug 23, 2012
    #16
  17. "Bill Cunningham" <> writes:
    [...]
    > I mean the deeper aspects of C. Some things at first you have to take on
    > faith before it finally clicks for good. For example,
    >
    > printf("%i\n",*px); is the same as
    > printf("%p\n",(void*)px);


    No, it isn't the same thing at all, and the difference is *exactly*
    what we've been discussing.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 23, 2012
    #17
  18. Keith Thompson wrote:

    > No, it isn't the same thing at all, and the difference is *exactly*
    > what we've been discussing.


    Whew. Let me go back and read the thread I know the first example works for
    me now.

    B
    Bill Cunningham, Aug 23, 2012
    #18
  19. Keith Thompson wrote:

    > No, it isn't the same thing at all, and the difference is *exactly*
    > what we've been discussing.


    Ok I read Kenneth Brody's post. I guess the difference is the pointer and
    what it's pointing at.

    Bill
    Bill Cunningham, Aug 23, 2012
    #19
  20. Bill Cunningham

    Mark Bluemel Guest

    On 23/08/2012 17:55, Kenneth Brody wrote:
    > On 8/22/2012 9:26 PM, Bill Cunningham wrote:
    >> Keith Thompson wrote:
    >>
    >>> No, it isn't the same thing at all, and the difference is *exactly*
    >>> what we've been discussing.

    >>
    >> Ok I read Kenneth Brody's post. I guess the difference is the pointer and
    >> what it's pointing at.

    >
    > Ding, ding, ding! We have a winner!
    >
    > That is precisely what most of this thread has been about. You have the
    > pointer itself, which is one thing, and what it points to, which is
    > obtained by dereferencing the pointer.
    >
    > I'd give an example using "1600 Pennsylvania Ave" and "The White House",
    > but I'm afraid that might just confuse you again.


    Do you really believe he's confused? You're a lot more charitable than me...
    Mark Bluemel, Aug 24, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard
    Replies:
    0
    Views:
    378
    Howard
    Jul 1, 2003
  2. Jakob Bieling
    Replies:
    0
    Views:
    426
    Jakob Bieling
    Jul 1, 2003
  3. Joe Seigh
    Replies:
    18
    Views:
    822
    tom_usenet
    Sep 22, 2003
  4. Matthias Kaeppler

    Dereference Adaptor

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    3
    Views:
    449
    Kurt Krueckeberg
    Feb 28, 2005
  5. Denis Palmeiro

    NULL Pointer Dereference

    Denis Palmeiro, Jul 8, 2003, in forum: C Programming
    Replies:
    10
    Views:
    651
    Shill
    Jul 16, 2003
Loading...

Share This Page