pointer assignments

Discussion in 'C Programming' started by rahul8143@gmail.com, Jul 25, 2005.

  1. Guest

    hello,
    i want to know what is the difference between following 2 snippets
    1)
    {
    int *p;
    int *q;
    p=q;
    ...
    ...
    }

    2)
    {
    int *q;
    int *p=q;
    ...
    ...
    }

    regards,
    rahul
     
    , Jul 25, 2005
    #1
    1. Advertising

  2. Cong Wang Guest

    wrote:
    > hello,
    > i want to know what is the difference between following 2 snippets
    > 1)
    > {
    > int *p;
    > int *q;
    > p=q;
    > ...
    > ...
    > }
    >
    > 2)
    > {
    > int *q;
    > int *p=q;
    > ...
    > ...
    > }
    >
    > regards,
    > rahul

    Yeah,they are really different.The first one is that:you assign the
    value of 'q'(an address) to 'p' which already has a random value.The
    second one is that: you declare an int* variable p,at the same time you
    assign the value of 'q' to it.The following statment shows the
    differet:
    void func(int i){
    static j=10;
    j+=i;
    }
    It is different from this:
    void func(int i){
    static j;
    j=10;
    j+=i;
    }
     
    Cong Wang, Jul 25, 2005
    #2
    1. Advertising

  3. Alipha Guest

    #1 creates two pointers and leaves their values uninitialized. then q
    is assigned to p.
    #2 q is left uninitialized and p is initialized with q's value.
    For all practical purposes, they are identical.
    Note, however, that I believe both snippets are undefined behavior
    because it is illegal to use the value of a variable that has been
    uninitialized (and assigning its value to another variable or
    initializing another variable with its value would be considered
    "use".)

    int *q = 0; /* or NULL if you prefer */

    would make both snippets legal.
     
    Alipha, Jul 25, 2005
    #3
  4. Flash Gordon Guest

    Alipha wrote:
    > #1 creates two pointers and leaves their values uninitialized. then q
    > is assigned to p.


    <snip>

    Please quote some context so that people know what you are replying to.
    Usenet works in such a way that people may *never* see the message you
    are replying to and, on it's own, you message makes absolutely no sense.

    Had you been reading this group for a while (which you should always do
    before posting), you would have seen lots of instruction on how to do
    this. Check CBFalconer's signature for one set of instructions.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jul 25, 2005
    #4
  5. pete Guest

    Cong Wang wrote:
    >
    > wrote:
    > > hello,
    > > i want to know what is the
    > > difference between following 2 snippets
    > > 1)
    > > {
    > > int *p;
    > > int *q;
    > > p=q;
    > > ...
    > > ...
    > > }
    > >
    > > 2)
    > > {
    > > int *q;
    > > int *p=q;
    > > ...
    > > ...
    > > }
    > >
    > > regards,
    > > rahul

    > Yeah,they are really different.


    Is that sarcasm?

    > The first one is that:you assign the
    > value of 'q'(an address) to 'p' which already has a random value.The
    > second one is that: you declare an int* variable p,
    > at the same time you
    > assign the value of 'q' to it.The following statment shows the
    > differet:
    > void func(int i){
    > static j=10;
    > j+=i;
    > }
    > It is different from this:
    > void func(int i){
    > static j;
    > j=10;
    > j+=i;
    > }


    I can't understand what you're getting at.
    Your functions have no return value
    and the side effect is unreadable.
    The static keyword changes your code example
    substantially from OP's example.

    --
    pete
     
    pete, Jul 25, 2005
    #5
  6. Denis Kasak Guest

    Cong Wang wrote:
    >
    > Yeah,they are really different.The first one is that:you assign the
    > value of 'q'(an address) to 'p' which already has a random value.The
    > second one is that: you declare an int* variable p,at the same time you
    > assign the value of 'q' to it.The following statment shows the
    > differet:
    > void func(int i){
    > static j=10;
    > j+=i;
    > }
    > It is different from this:
    > void func(int i){
    > static j;
    > j=10;
    > j+=i;
    > }


    The example you used is really not a good analogy to the OP's question.
    In your example 'j' is a static variable and static variables get
    initialized only once. Because of that, the first snippet will assign to
    'j' the value of 10 only once, and the second will assign it on every
    entrance to 'func'. This is not equivalent to the OP's case where both
    variables were non-static.

    -- Denis
     
    Denis Kasak, Jul 25, 2005
    #6
  7. Cong Wang Guest

    Denis Kasak wrote:
    > Cong Wang wrote:
    > >
    > > Yeah,they are really different.The first one is that:you assign the
    > > value of 'q'(an address) to 'p' which already has a random value.The
    > > second one is that: you declare an int* variable p,at the same time you
    > > assign the value of 'q' to it.The following statment shows the
    > > differet:
    > > void func(int i){
    > > static j=10;
    > > j+=i;
    > > }
    > > It is different from this:
    > > void func(int i){
    > > static j;
    > > j=10;
    > > j+=i;
    > > }

    >
    > The example you used is really not a good analogy to the OP's question.
    > In your example 'j' is a static variable and static variables get
    > initialized only once. Because of that, the first snippet will assign to
    > 'j' the value of 10 only once, and the second will assign it on every
    > entrance to 'func'. This is not equivalent to the OP's case where both
    > variables were non-static.
    >
    > -- Denis


    Yeah,I just show an example to prove that difference.It is bad code for
    I didn't think too much of this.
     
    Cong Wang, Jul 25, 2005
    #7
  8. Steve Summit Guest

    rahul8143 writes:
    > i want to know what is the difference between following 2 snippets
    > 1)
    > int *p;
    > int *q;
    > p=q;
    > 2)
    > int *q;
    > int *p=q;


    Very, very little difference. Unless there's something tricky
    you have in mind, the two snippets are for practical purposes
    identical in effect.

    If you're wondering about the asymmetry, why it is that (1) says
    "p=q" while (2) seems to say "*p=q", you're right, that is a
    little odd. Rest assured that it's p you're initializing in (2),
    not *p. (Question 4.2 in the book-length version of the FAQ list
    talks about this asymmetry.)

    Steve Summit
     
    Steve Summit, Jul 25, 2005
    #8
  9. Denis Kasak Guest

    Cong Wang wrote:
    >
    > Yeah,I just show an example to prove that difference.It is bad code for
    > I didn't think too much of this.


    But the problem is that the examples the OP provided *were* equivalent
    in the sense of the outcomes produced by them, and yours were not,
    mainly because adding the 'static' keyword changes the situation
    considerably.

    The difference shown in your examples had nothing to do with the OP's
    question.

    -- Denis
     
    Denis Kasak, Jul 25, 2005
    #9
  10. Cong Wang Guest

    Denis Kasak wrote:
    > Cong Wang wrote:
    > >
    > > Yeah,I just show an example to prove that difference.It is bad code for
    > > I didn't think too much of this.

    >
    > But the problem is that the examples the OP provided *were* equivalent
    > in the sense of the outcomes produced by them, and yours were not,
    > mainly because adding the 'static' keyword changes the situation
    > considerably.
    >
    > The difference shown in your examples had nothing to do with the OP's
    > question.
    >
    > -- Denis

    Oh? Why changes the situation? Can you say more?
     
    Cong Wang, Jul 26, 2005
    #10
  11. Denis Kasak Guest

    Cong Wang wrote:
    >
    > Oh? Why changes the situation? Can you say more?


    I already explained it in my first reply, so you should check it out one
    more time.

    Basically, without the static keyword, the variable would be initialized
    upon each entrance of the function making it irrelevant whether you will
    initialize the variable with the said value, or set it to that same
    value immediately after the declaration. The static keyword changes this
    behaviour.

    -- Denis
     
    Denis Kasak, Jul 26, 2005
    #11
  12. Mabden Guest

    "mike" <> wrote in message
    news:de5svf$abq$...
    >
    > static variables are 3v1l!


    What a silly thing to say. I suppose recursion is evil as well?

    --
    Mabden
     
    Mabden, Aug 23, 2005
    #12
  13. Zoran Cutura Guest

    Mabden <mabden@sbc_global.net> wrote:
    > "mike" <> wrote in message
    > news:de5svf$abq$...
    >>
    >> static variables are 3v1l!

    >
    > What a silly thing to say. I suppose recursion is evil as well?


    And goto is evil, isn't it. ;-)

    Actually coding is evil. No matter how you do it. ;-)

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Aug 23, 2005
    #13
  14. Alan Balmer Guest

    On Sat, 20 Aug 2005 03:19:51 +0300, mike <>
    wrote:

    >Denis Kasak wrote:
    >> Cong Wang wrote:
    >>
    >>>
    >>> Oh? Why changes the situation? Can you say more?

    >>
    >>
    >> I already explained it in my first reply, so you should check it out one
    >> more time.
    >>
    >> Basically, without the static keyword, the variable would be initialized
    >> upon each entrance of the function making it irrelevant whether you will
    >> initialize the variable with the said value, or set it to that same
    >> value immediately after the declaration. The static keyword changes this
    >> behaviour.
    >>
    >> -- Denis
    >>
    >>

    >
    >static variables are 3v1l!


    N07 n34r|y 45 3v1| 45 |337-5p34k.

    Ch1|dr3n 5h0u|d 4v01d U53n37 un71| 7h3y |34rn 3n6|15h.
    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 23, 2005
    #14
    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. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,269
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    639
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    457
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page