Just value copied?

Discussion in 'C Programming' started by Garma, Jan 16, 2004.

  1. Garma

    Garma Guest

    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!
     
    Garma, Jan 16, 2004
    #1
    1. Advertising

  2. Garma <> 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 ()
     
    Alex Monjushko, Jan 16, 2004
    #2
    1. Advertising

  3. On Fri, 16 Jan 2004 00:03:16 GMT, "Garma" <>
    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
     
    Horst Kraemer, Jan 16, 2004
    #3
  4. 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);
     
    E. Robert Tisdale, Jan 16, 2004
    #4
  5. Garma

    Jack Klein Guest

    On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
    <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 16, 2004
    #5
  6. On Fri, 16 Jan 2004 00:03:16 GMT, "Garma" <>
    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>>
     
    Barry Schwarz, Jan 16, 2004
    #6
  7. 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)++

    . . ."
     
    E. Robert Tisdale, Jan 16, 2004
    #7
  8. Garma

    pete Guest

    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
     
    pete, Jan 16, 2004
    #8
  9. Garma

    Richard Bos Guest

    Jack Klein <> wrote:

    > On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
    > <> 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
     
    Richard Bos, Jan 16, 2004
    #9
  10. Jack Klein wrote:

    > On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
    > <> 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 :
    "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
     
    Richard Heathfield, Jan 17, 2004
    #10
  11. Garma

    Joe Wright Guest

    Richard Heathfield wrote:
    >
    > Jack Klein wrote:
    >
    > > On Thu, 15 Jan 2004 16:23:17 -0800, "E. Robert Tisdale"
    > > <> 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 ---
     
    Joe Wright, Jan 17, 2004
    #11
    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. BH
    Replies:
    4
    Views:
    715
    sampsons
    Jul 17, 2003
  2. J Gao
    Replies:
    0
    Views:
    1,213
    J Gao
    Jul 28, 2003
  3. Carl Prothman [MVP]

    Re: No files copied using Copy Project

    Carl Prothman [MVP], Aug 12, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    554
    Christopher Campbell
    Aug 12, 2003
  4. Hans Kesting
    Replies:
    0
    Views:
    1,171
    Hans Kesting
    Jan 5, 2004
  5. Tropos
    Replies:
    1
    Views:
    310
    tom_usenet
    Dec 19, 2003
Loading...

Share This Page