dereference

B

Bill Cunningham

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;
}
 
K

Keith Thompson

Bill Cunningham said:
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?
 
B

Bill Cunningham

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


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.
 
L

Les Cargill

Bill said:
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.
 
B

Bill Cunningham

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


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
 
K

Keith Thompson

Bill Cunningham said:
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.
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.
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

Keith said:
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
 
K

Keith Thompson

Kenneth Brody said:
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.
 
B

Bill Cunningham

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
 
B

Barry Schwarz

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.
 
B

Bill Cunningham

Barry said:
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
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

Keith said:
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
 
B

Bill Cunningham

Keith said:
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
 
M

Mark Bluemel

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top