473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Just value copied?

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!


Nov 14 '05 #1
10 1426
Garma <Ga*****@nosysserv.com> wrote:
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'? p = &b;
This assigns the address of 'b' to 'p'.
*p = a;


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

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #2
On Fri, 16 Jan 2004 00:03:16 GMT, "Garma" <Ga*****@nosysserv.com>
wrote:
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'.

--
Horst

Nov 14 '05 #3
Garma wrote:
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);
Nov 14 '05 #4
On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
Garma wrote:
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.
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);


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
On Fri, 16 Jan 2004 00:03:16 GMT, "Garma" <Ga*****@nosysserv.com>
wrote:
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>>
Nov 14 '05 #6
Jack Klein wrote:
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)++

. . ."
Nov 14 '05 #7
E. Robert Tisdale wrote:

Jack Klein wrote:
There is no such thing as a reference in the C language.
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.

--
pete
Nov 14 '05 #8
Jack Klein <ja*******@spamcop.net> wrote:
On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
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.


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
Nov 14 '05 #9
Jack Klein wrote:
On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
*p is a reference to b -- *p is another name for b.

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>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #10
Richard Heathfield wrote:

Jack Klein wrote:
On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
*p is a reference to b -- *p is another name for b.

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.


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?
:-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tropos | last post by:
Query: Will a MutexGuard object release before a function return value is copied? Consider the C++ code: class MutexGuard //A familiar sort of class for making mutexes exception-safe { . ....
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
5
by: lkrubner | last post by:
Is it true that Javascript has no clone() method, to pass an object by copy of value instead of reference? If I have an object and want to make an array out of all of its instance variables, I...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
17
by: minlar | last post by:
Hello,everyone: what the value of the variables in the next programe: { int x=35; char str; strcpy(str,"www.google.com");} what's the value of x and strlen(str)? any help is welcome, and I am...
5
by: Dariusz Bismor | last post by:
Hi, Please help me to understand compiler behavior concerning the following code: class X{    int k; public:    X(){ k=1; }    X( X & model ){       k = model.k;
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.