Just value copied?

G

Garma

In this code,

int main(){
int a = 47;
int b;
int* p;

p = &b;
*p = a;
return 0;
}

Is there any problem of the code in C?
In statement of "*p = a;", is just value of 'a' copied to the memory address
of 'p' or 'p' re-pointed to address of 'a'?
Thanks for your comments!
 
A

Alex Monjushko

Garma said:
In this code,
int main(){
int a = 47;
int b;
int* p;
p = &b;
*p = a;
return 0;
}
Is there any problem of the code in C?

No problem.
In statement of "*p = a;", is just value of 'a' copied to the memory address
of 'p' or 'p' re-pointed to address of 'a'?

This assigns the address of 'b' to 'p'.

This deferences 'p' and assigns the value of 'a' to whatever
'p' points to, namely 'b'. Hence, the value of 'b' changes but
'p' itself remains unchanged since it still contains the address
of 'b'.
 
H

Horst Kraemer

In this code,

int main(){
int a = 47;
int b;
int* p;

p = &b;
*p = a;
return 0;
}

Is there any problem of the code in C?
No.

In statement of "*p = a;", is just value of 'a' copied to the memory address
of 'p'

it isn't stored to the memory address *of* 'p' but to the memory
address *contained* in p, i.e. the value of 'a' is stored in fact to
'b'.
or 'p' re-pointed to address of 'a'?

No.

*p = a;

stores the value of 'a' to the object 'p' is pointing to - i.e. to 'b'
- and

p = &a;

would re-point 'p' to 'a'. It stores the address of 'a' to 'p'.
 
E

E. Robert Tisdale

Garma said:
In this code,

int main(int argc, char* argv[]){
int a = 47;
int b;
int* p = &b; // p <-- address(b)
*p = a; // b <-- a
return 0;
}

Is there any problem of the code in C?
No.

In the statement "*p = a;",
is the value of 'a' just copied to the memory address of 'p'?

No.
*p is a reference to b -- *p is another name for b.

*p = a;

is the same as

b = a;
Or 'p' re-pointed to address of 'a'?

No. You would write

p = &a;

to "reseat" pointer p so that it points to a.

You must write

p = (int*)a;

to assign p the value of a (47);
 
J

Jack Klein

Garma said:
In this code,

int main(int argc, char* argv[]){
int a = 47;
int b;
int* p = &b; // p <-- address(b)
*p = a; // b <-- a
return 0;
}

Is there any problem of the code in C?
No.

In the statement "*p = a;",
is the value of 'a' just copied to the memory address of 'p'?

No.
*p is a reference to b -- *p is another name for b.

No, it is not a reference to b, it is a pointer that currently points
to b. There is no such thing as a reference in the C language.
*p = a;

is the same as

b = a;

No, it has the same effect as "b = a;". The execution might or might
not be the same, only the result is.
No. You would write

p = &a;

to "reseat" pointer p so that it points to a.

You must write

p = (int*)a;

to assign p the value of a (47);

Where does it say that such an assignment, with a cast, will result in
a pointer having the value of 47? The result of such an assignment is
implementation-defined, but might well involve a completely different
bit pattern. And of course any attempt to use the pointer results in
undefined behavior.
 
B

Barry Schwarz

In this code,

int main(){
int a = 47;
int b;
int* p;

p = &b;
*p = a;
return 0;
}

Is there any problem of the code in C?
In statement of "*p = a;", is just value of 'a' copied to the memory address
of 'p' or 'p' re-pointed to address of 'a'?

Neither. The value of a is copied to the area pointed to by p, which
in this case is b. The value stored in p is unchanged.

If you wanted p to point to a, then you would need p = &a;

If you wanted the value of a to be copied to the memory address of p,
that is to be placed in the memory that p occupies, you would need
p = (int*)a; but this is not guaranteed to work if 47 is not a valid
address for an int.


<<Remove the del for email>>
 
E

E. Robert Tisdale

Jack said:
There is no such thing as a reference in the C language.

Tell that to Brian W. Kernighan and Dennis M. Ritchie,
"The C Programming Language", Chapter 5: Pointers and Arrays,
Section 1: Pointers and Addresses, page 90-1:

"Pointer references can also occur on the left side of assignments.
If px points to x, then

*px = 0

sets x to zero, and

*px += 1

increments it, as does

(*px)++

. . ."
 
P

pete

E. Robert Tisdale said:
Tell that to Brian W. Kernighan and Dennis M. Ritchie,

It's too late now.
"The C Programming Language", Chapter 5: Pointers and Arrays,
Section 1: Pointers and Addresses, page 90-1:
"Pointer references can also occur on the left side of assignments.

If it would have been in K&R2,
it would have been on page 94, but it's not.
 
R

Richard Bos

Jack Klein said:
Where does it say that such an assignment, with a cast, will result in
a pointer having the value of 47? The result of such an assignment is
implementation-defined, but might well involve a completely different
bit pattern.

The bit pattern is immaterial; the same thing is true for

float f;
int a=47;

f=a;

In fact, the quoted line does assign to p, not exactly 47, but perhaps a
better phrasing is "that pointer value which has some relation to the
value 47". It's not quite the same thing as the int 47; but it should be
semantically related, if at all possible on the underlying architecture.
And of course any attempt to use the pointer results in
undefined behavior.

Of course.

Richard
 
R

Richard Heathfield

Tisdale is of course wrong about *p being a reference to b. If anything, it
is p, not *p, that refers to b. See below.
No, it is not a reference to b, it is a pointer that currently points
to b. There is no such thing as a reference in the C language.

Counter-example from 6.2.5 of C99:

"A pointer type may be derived from a function type, an object type, or an
incomplete type, called the referenced type. A pointer type describes an
object whose value provides a reference to an entity of the referenced
type."

<snip>
 
J

Joe Wright

Richard said:
Tisdale is of course wrong about *p being a reference to b. If anything, it
is p, not *p, that refers to b. See below.

Wrong. I think you mean "pass by reference" of variables as function
parameters. It is clear that pointers 'reference' other objects.
Counter-example from 6.2.5 of C99:

"A pointer type may be derived from a function type, an object type, or an
incomplete type, called the referenced type. A pointer type describes an
object whose value provides a reference to an entity of the referenced
type."
See I told you. Pointers are objects! Where did I put that old thread?
:)
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top