pointer

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Ok I have

int x;
int y;
int *px;
int *py;
py=&y;
y=x;

Have I changed the value of the x int with the last expression or have I
changed the point py to point to x? I'm still hung up for the most part now
with dereferencing. Should I have used

*y=x; ?

Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok I have

int x;
int y;
int *px;
int *py;
py=&y;

This assigns the address of y to the pointer object py; the value
of py is now &y, so *py becomes another way to refer to y.

This simply copies the value of x (which you didn't initialize) to y.
It has nothing to do with any pointers.
Have I changed the value of the x int with the last expression

No, you didn't change the value of x.
or have I
changed the point py to point to x?

No, you haven't done that either.
I'm still hung up for the most part now
with dereferencing. Should I have used

*y=x; ?

No, that wouldn't make any sense; y isn't a pointer, so you can't
apply the "*" dereferencing operator to it.
 
B

Bill Cunningham

[snip]
No, that wouldn't make any sense; y isn't a pointer, so you can't
apply the "*" dereferencing operator to it.

What about
*py=x;

That would change *py to point to the value of x which isn't initialized
right?

int x=0;
int y=1;
int *py;
int *px;
py=&y;
px=&x;

To change y to 2 by using a pointer, how would I do that? Does that
involve the dereferencing?

Bill
 
K

Keith Thompson

Bill Cunningham said:
[snip]
No, that wouldn't make any sense; y isn't a pointer, so you can't
apply the "*" dereferencing operator to it.

What about
*py=x;

That would change *py to point to the value of x which isn't initialized
right?

No, *py is an int, so it doesn't point to anything.

py = &y; /* change py so it points to y */
*py = x; /* This doesn't change py; it changes value of the
int object *py (remember that *py is an alias for y). */
int x=0;
int y=1;
int *py;
int *px;
py=&y;
px=&x;

To change y to 2 by using a pointer, how would I do that? Does that
involve the dereferencing?

*py = 2;
 
B

bpascal123

Bill Cunningham said:
*y=x;        ?
No, that wouldn't make any sense; y isn't a pointer, so you can't
apply the "*" dereferencing operator to it.
    What about
*py=x;
That would change *py to point to the value of x which isn't initialized
right?

No, *py is an int, so it doesn't point to anything.

py = &y; /* change py so it points to y */
*py = x; /* This doesn't change py; it changes value of the
            int object *py (remember that *py is an alias for y). */
int x=0;
int y=1;
int *py;
int *px;
py=&y;
px=&x;
    To change y to 2 by using a pointer, how would I do that? Does that
involve the dereferencing?

*py = 2;

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


(((===o===)))

what about : *py += 1 ; /* it just adds 1 to the value or variable of
*py */

Warning : I am learning about pointers too...

(((===o===)))
 
B

Bill Cunningham


Ok with the above example. Am I changing the py pointer to an int or the
value of int y? Remembering that *py simply points to an address in memory,
does *py=2 change the stored value in memory address int y ?

Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok with the above example. Am I changing the py pointer to an int or the
value of int y? Remembering that *py simply points to an address in memory,
does *py=2 change the stored value in memory address int y ?

Given the previous assignment:
py = &y;
the statement:
*py = 2;
is equivalent to:
y = 2;
 
B

Beej Jorgensen

Bill Cunningham said:

"py is assigned the address of y", or equivalently, "py points to y." Y
hasn't been changed, nor has x; only py has changed to now point to y.

"y is assigned x." Nothing magical or pointery here.

In general, to get a pointer to a variable, use the address-of operator
on the variable. To do the reverse (to get a variable from a pointer to
that variable), use dereference.

1| int *p; // p is a pointer to an int
2| int x; // x is an int
3| int y; // y is an int
4|
5| p = &x; // &x is a pointer to an int (namely a pointer to x)
6|
7| y = *p; // *p is an int (namely x)

(Don't get the "*" on line 1 (the declaration of p) confused with the one
on line 7 (dereferencing p)--they have different meanings based on
context.)

If you'd like to change "y" at this point, you have two options. One is
to assign directly to it, like you did with "y=x". The other is to go
through "py", since you've set py up to point to y. This is where the
dereference would come in:

{
int x, y, *py; // not a dereference, just a declaration

y = 1; // normal assignment
printf("%d\n", y); // "1"

py = &y; // py points to y
printf("%d\n", *py); // dereference py to see the value of y: "1"

y = 2; // normal assignment
printf("%d\n", *py); // dereference py to see the value of y: "2"
printf("%d\n", y); // print y directly: "2"

*py = 3; // assignment into whatever py points at, namely y
printf("%d\n", y); // print y directly: "3"

x = 3490;
py = &x; // py now points to x
*py = 6502; // assign 6502 into whatever py points at, namely x
printf("%d\n", y); // y is still "3" from before
printf("%d\n", *py); // py points to x, so "6502"
printf("%d\n", x); // print x directly, "6502"
}

-Beej
 
B

Bill Cunningham

[snip]
y = 2; // normal assignment
printf("%d\n", *py); // dereference py to see the value of y: "2"
printf("%d\n", y); // print y directly: "2"

*py = 3; // assignment into whatever py points at, namely y
printf("%d\n", y); // print y directly: "3"

x = 3490;
py = &x; // py now points to x
*py = 6502; // assign 6502 into whatever py points at, namely x
printf("%d\n", y); // y is still "3" from before
printf("%d\n", *py); // py points to x, so "6502"

On this above line I would probably written
printf("%i\n",py);
and come up with hard to tell. And I know you can look at addresses too ;)

printf("%p\n",py);

Bill
 
K

Keith Thompson

Bill Cunningham said:
On this above line I would probably written
printf("%i\n",py);
and come up with hard to tell. And I know you can look at addresses too ;)

And you would have been wrong. py is a pointer; printing it with "%i"
is incorrect.

Also, "%i" is equivalent to "%d", and "%d" is much more commonly used.
printf("%p\n",py);

That's not *as* wrong, but it's still wrong. "%p" expects a pointer
argument, and py is a pointer, but it's the wrong type of pointer.
"%p" requires a void*, and py is an int*. This is one of the rare
cases where a cast is actually appropriate:

printf("%p\n", (void*)py);

(Actually, char* is almost certainly interchangeable with void* in
this context, but it's easier to cast to void* than to remember the
reasons why char* is ok.)

[...]
 
B

Beej Jorgensen

Richard said:
Give up Bill. It's not for you.

That assessment is really up to Bill, not you.

I've tutored people who were really into programming (easy and fun),
people who were just in it for the money (not so easy), and people who
didn't know if they were into it or not.

One guy decided he wanted to get into programming, so we met for a
couple hours. After a while, he said, "So programming is basically
taking data, manipulating it, moving it around, and sometimes showing
some of it to the user in some format or another? Even first-person
shooters."

I said, "Well... yes, basically. The challenge is in solving a problem
such that you move the data correctly and efficiently."

He said, "Hmmm. I'm not really interested in that, I don't think."

Later on, he went back to school and studied graphic arts and
typography; recently he thanked me for the insight, which I consider a
win.

But I never told him he couldn't do it.

-Beej
 
D

Default User

Beej said:
That assessment is really up to Bill, not you.

I've tutored people who were really into programming (easy and fun),
people who were just in it for the money (not so easy), and people who
didn't know if they were into it or not.

I'll point out that Bill has supposedly been learning C for about ten
years now. If he's no further along that he is, then either he is
unable to learn it or is engaging in a "long con" troll.

I vowed to stop mentioning this to Keith, and I won't bring it up to
you again either.




Brian (pained to agree with Richard)
 
B

Beej Jorgensen

Default User said:
I'll point out that Bill has supposedly been learning C for about ten
years now. If he's no further along that he is, then either he is
unable to learn it or is engaging in a "long con" troll.

Hmmm. Well, I'll absolutely entertain both those notions, but telling
him to "give it up" seems like a long shot no matter what his
motivations are.

If he is a long con troll, I apologize to Richard for assuming he was
being too harsh.

-Beej
 
B

Beej Jorgensen

Richard said:
So have I. And very well reviewed too if I say so myself. However, if he
doesn't get it now he never will. But he's a troll or incredibly not
suited for programming.

Just Google his posts.

It's OK, I'll take your word for it, and I'm sorry for thinking you were
being too harsh.

-Beej
 
B

Ben Bacarisse

Richard said:
You can take a horse to water but you cant force him to drink.

Bill drinks deep at the comp.lang.c well. I don't know to what extent
he is refreshed, but he does drink.
Bill is incapable of being a programmer if he is not a troll. This is a
fact. Encouraging him would be akin to telling someone with no legs they
can win an able bodied marathon. It's simply not going to happen. if he
still doesn't understand pointers in C after years of "learning" then
its game over. You would be cruel to encourage him.

What a revealing view of the world: only people capable of winning
should be encouraged. I'd argue in favour of encouraging
participation in sport even for those with no change of winning
anything. Maybe they will simply enjoy taking part.

Bill may get considerable benefit from learning C, even with nothing
that you would call "progress".
 
B

Beej Jorgensen

Richard said:
I just thought you needed a heads up since it can be easy to be pulled
in.

I'm not too worried about that. And in fact, I'll probably still
respond to him if I feel I can write something helpful for whomever
might be reading.

-Beej
 
B

Barry Schwarz

Bill drinks deep at the comp.lang.c well. I don't know to what extent
he is refreshed, but he does drink.

Not really. He posts bogus code, ignores the advice others provide,
and suggests a random guess at a solution.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top