Thread: Pointer problem

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    Pointer problem

    We just got in pointers in my class, and i am facing some difficulties in understanding them.

    Here's the code with the problem:
    Code:
    #include <stdio.h>
    
    main()
    {
    	int *p;
    	int k = 5;
    
    	p = &k;
    
    	printf( "%p, p* = %d, p+1 = %p, %d ", p, *p, p+=1, *p );
    }
    What i expect this programm to do is write firstly the address of p, then 5, then the new adress and then a number i don't know in advance.
    However the programm prints the adress, then the number i don't know in advance then the new adress and then 5.

    Why?

    Thx - Polor

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Reply

    Hi friend,
    Note that C does operation from right to left and not from left to right as you expect. This is because, pushing and poping of elements takes place in reverse order of pascal.
    In short, if i=10 then,
    printf("%d %d %d %d",p,++p,p++,++p); will print
    13,13,11,11and (go from right)
    not as
    10,11,11,13.(go from left--wrong)
    Saravanan.T.S.
    Beginner.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    I don't completey understand it...

    I know that anything might happen to the values, but the problem is the way it prints the values. My tutor didn't told us this example. He told us that if you write somewhere p+=1, *p might get any value. And i wrote this programm to experiment a little with it and i faced that problem.

    >printf( "%p, p* = %d, p+1 = %p, %d ", p, *p, p+=1, *p );
    That means that:
    1) print the value of *p,
    2) add 1 to p and print the new address,
    2) print th new *p (unkown value )
    4) print again p

    That's what do you mean?

    >Looks like saravanan_ts has fallen into the same tar-pit as well.
    what has seravan doen wrong.?


    >printf("%d %d %d %d",p,++p,p++,++p); will print
    >13,13,11,11and (go from right)
    Yeah, i checked this and the outpout is as you said. But why? Let's say it prints it from right.
    First it should print ++p which is 11, then p++ which is 11, then ++p which is 13 and the p which is 13.
    ???


    ------------------------

    Another question:
    Code:
    #include <stdio.h>
    
    main()
    {
    	int *p;
    	printf( "int = %d, p = %d ", sizeof( int), sizeof( p ) );
    }
    why when i have sizeof( p ) it prints 4, and when i have sizeof( *p ) it prints 2??????? { sizeof int is 2 }

    And why if i declare *p of type long, both sizeof( p ) and sizeof( *p ) prints 4 ???

    Thx for any help - Polor

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    printf( "%p, p* = %d, p+1 = %p, %d ", p, *p, p+=1, *p );
    If you really want to test all these operations, because ot the interaction of these ops as Salem et al mentioned, break the statement up so you don't have those problems:

    printf( "%p,", p);
    printf("p* = %d, ", *p);
    printf("p+1 = %p", p+=1);
    printf("%d ", *p );

    This will prevent the undefined interaction between the values, but not the undefined problems such as p+1 introduce.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Lightbulb Hi

    As there are some persons who have no other duties other than posting, they can provide all links. But to my little knowledge,
    there is one thing called pascal.
    if you put word pascal in front of any function, the parameters will be taken from left to right but in Cdecl it is from right to left.
    All these stuffs are in a Book called "HIDDEN TREASURES IN C".
    Saravanan.T.S.
    Beginner.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    I need more help!

    Well, your example says about x++ and ++x, not about x+=1. x+=1 doesn't have the same problem as with ++, as my teacher tells me, so why to have this problem with my code?

    >But that is only in this example - you should not assume that every compiler will treat every function call in the same manner.

    Grrrrrrrrrrrr!!! And where the **** will i know that it will be used from left to right or from right to left ( i am talking about my own example )??? That's very annoying, because as my tutor told us, we should only use standard things which apply to all compilers.

    Here's one question i posted before but i did not get an answer:

    >>printf( "%p, p* = %d, p+1 = %p, %d ", p, *p, p+=1, *p );
    >That means that:
    >1) print the value of *p,
    >2) add 1 to p and print the new address,
    >2) print th new *p (unkown value )
    >4) print again p
    >
    >That's what do you mean?

    And about Salem's example, i understand it. It's ok with it.

    ---------------------------------

    For the second example, you mean that the sizeof( *p ) is 2 because in my coputer the object that p points to is of type int which has size 2?

    And p by its own has size 4? Pointers have size?

    Thanks,
    I need more help with my initiall questions on problem 1 please
    - Polor

  7. #7
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Re: I need more help!

    Originally posted by pizzas
    Well, your example says about x++ and ++x, not about x+=1. x+=1 doesn't have the same problem as with ++, as my teacher tells me, so why to have this problem with my code?
    Your teacher is referring to post- and prefix. But in your example it can't make things much worse than they allready are, if you use p+=1 or ++p or p++.
    The order in which arguments to functions are evaluated is undefined.
    Get yourself a copy of the ANSI ISO/IEC 9899:1999 C-Standard and look up the "Comma operator".
    In contrast the logical && and || operators guarantee left-to-right evaluation!

    And also remember: You are only allowed to assign a value to a variable ONCE between two semicolons.
    Code:
    printf("%d %d %d %d",p,++p,p++,++p);
    So, the behaviour of the code above is undefined ... but Salem already pointed that out.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    Only this?????

    Is that the only thing you have to say, for all of my above questions????

    >printf("%d %d %d %d",p,++p,p++,++p);
    i understand why it is undefined, but i don't use x++ and ++x, I use x+=1.

    Hey Salem could you give me a hand of help pls?

    Waiting for more replies - Polor

  9. #9
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Re: Only this?????

    Originally posted by pizzas
    Is that the only thing you have to say, for all of my above questions????
    Sorry for trying to point some importing things out. No need to get rude.
    There is no point in discussing undefined source code, because its behaviour is undefined.

    >>printf("%d %d %d %d",p,++p,p++,++p);
    >i understand why it is undefined, but i don't use x++ and ++x, I use x+=1.

    Well, you seem not to understand. If you use x+= 1 instead of ++x or x++ where is the difference? You are still assigning a new value to x.


    For the second example, you mean that the sizeof( *p ) is 2 because in my coputer the object that p points to is of type int which has size 2?

    correct

    >And p by its own has size 4? Pointers have size?
    Yes, of course. Pointers store the address they are pointing to. You need 4 byte on 32bit architectures. But I am sure Salem can explain that much better than me.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I have been reading something online about pointers... Is anyone here familiar with this guy's material? Is he saying the right stuff?

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    ++ is out of point. The problem is:

    I am afraid that noone ( ecxept WaltP i think ) has understood the problem.

    ---------->>
    I know that i add 1 to p and that the new value is not known in advance, it is "random". I know that it is undifined. I know that!!!!!
    <<--------------

    But my problem is why
    printf( "%p, p* = %d, p+1 = %p, %d ", p, *p, p+=1, *p );

    will print this:
    Address_of_p+1 Unkown_value Address _of_p+1 5
    and not this:
    Address_of_p 5 Address _of_p+1 Unkown_value

    ???????

    You said that because it does the code from right to left. And that this is not for all computers.
    Question: What applies to all computers? What's the answer??

    You also mentioned ++ causes undefined behaviour, but i don't use ++, but +=.
    But this isn't my problem right now.

    thx - Polor

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    2 last questions

    I don't know if i understand it not.. ( but for some reson i think i did..)

    The link you posted, Salem, previously, says only about ++ and not about +=. But ok.

    I tried your piece of code, and it worked as i expected it, since either by going right to left in printf, either by going left to right, the result will be always the same.

    So my quetion: In the printf statmenet, the values are proccessed right to left, or left to right?????????????
    ( if it is right to left, then the output i get i think is normal ).

    ------
    Question 2: Why when i add 1 to p the new address is incremented by 2??????????????????????? Does it have anything to do with int, which has size 2 in my computer ?

    Thx for your attempt Salem and thx to all the others who tried to help me
    - Polor

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: 2 last questions

    Originally posted by pizzas
    I don't know if i understand it not.. ( but for some reson i think i did..)

    The link you posted, Salem, previously, says only about ++ and not about +=. But ok.

    I tried your piece of code, and it worked as i expected it, since either by going right to left in printf, either by going left to right, the result will be always the same.

    So my quetion: In the printf statmenet, the values are proccessed right to left, or left to right?????????????
    ( if it is right to left, then the output i get i think is normal ).
    It should not matter. As long as you don't change the values while printing, right or left will give you the same answer. Don't reference any values twice if they are changed within the statement
    ------
    Question 2: Why when i add 1 to p the new address is incremented by 2??????????????????????? Does it have anything to do with int, which has size 2 in my computer ?
    Yes, the compiler knows that the pointer is pointing to an int, so it's smart enough to ++ based on the type size, to the next int. If it only incremented by 1, you'd be pointing to the middle of an int so the compiler compensates.

    Thx for your attempt Salem and thx to all the others who tried to help me
    - Polor
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    There is always a "but" ..!

    >It should not matter. As long as you don't change the values while printing, right or left will give you the same answer.

    Yeah, it should not matter only if i don't change the values ( like Salem's piece of code ).
    But now i change it; so, is it right to left or left to right ?

    Thx - Polor

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: There is always a "but" ..!

    >But now i change it; so, is it right to left or left to right ?

    The order in which the function designator, arguments, and subexpressions within the arguments are evaluated in a function call is unspecified behavior.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM