Thread: While loop problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    While loop problem

    Hi. This is my first post. I'm a complete beginner with C, but have some ability with Python, and know a little Java.

    Anyway, my problem involves while loops and pointers. I'm just getting the hang of pointers and have been making a few mess-around programs. One such program is supposed to iterate a pointer.

    Code:
    int x;
    int *y;
    
    x = 500; //arbitrary value.
    y =&x;
    
    printf("%d", x); //print 500
    printf("%d", y); // print memory space value.
    
    *y = 200;
    printf("%d",x); //print 200
    //the above stuff is fine. Its the below while loop that I have a problem with. 
    *y = 1;
    while( *y<10){
    printf("%d",x);
    *y++
    }
    In the while loop, I expected the output to be 1,2,3,4...9. But it doesn't seem to loop. I don't know why. It compiles fine, but the output is simply 1. Then it stops.

    I'm sure I'm making a completely stupid mistake, but as a "N00b" can't figure out what is wrong.
    Last edited by SimpleMind; 07-10-2010 at 10:22 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The * dereferences the pointer. You should change the last part to "y++" instead of "*y++".


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thanks, but I'm not sure I understand.
    Should I initialize y to 1?
    But y refers to the memory space holding x (Im probably mutilating the jargon there).
    If I iterate through y how will this change the value of x?

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    > Should I initialize y to 1?
    Assuming you mean *y, yes, unless you want it to equal 200.

    > But y refers to the memory space holding x
    Yes, and the * tells the compiler that the operator(+-/*) should affect the memory location referred to by y, instead of y itself.

    > If I iterate through y how will this change the value of x?
    If you mean *y, then yes.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I find what you're actually doing to be kind of odd but your actual problem is that the postfix increment operator is higher on the operator precedence table than the dereference operator (++ and * respectively).

    *y++; will move the pointer first, then dereference. Well placed parentheses will do the right thing.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Ah... I think I see what you mean. I should write (*y)++, perhaps. I'll give it a try later. (I'm using a different computer nat the moment, with no compiler.)

    As to the purpos eof what I'm doing, well there isn't really one. Other than to mess around with pointers. I'm still waiting for Amazon to deliver my C and C++ books, so I'm messing around getting used to the syntax first.

    Thanks for the help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by whiteflags View Post
    *y++; will move the pointer first, then dereference.
    No it doesn't.
    Code:
    #include<stdio.h>
    int main( void )
    {
        char foo[] = "hello world";
        char *p = foo;
        char c = *p;
        
        printf( "%p : %c : %c : %s\n", p, c, *p, foo );
        c = *p++;
        printf( "%p : %c : %c : %s\n", p, c, *p, foo );
        c = ++*p;
        printf( "%p : %c : %c : %s\n", p, c, *p, foo );
        c = *++p;
        printf( "%p : %c : %c : %s\n", p, c, *p, foo );
    
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Oh, right. Well hopefully that will make things clear. The parens then is just about incrementing the pointed to value the postfix way.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Operator precedence
    If y is pointer, and you want to increment *what it points to*
    use
    (*y)++;
    ++*y;
    *y += 1;
    *y = *y + 1;

    But not
    *y++; //deference what pointer points & increment pointer p.


    >*y++; will move the pointer first, then dereference. Well placed parentheses will do the right thing.
    @whiteflag, hum?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Read the thread.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    I did as said and used (*y)++ instead of *y++. It works perfectly. Thanks everyone.

    Incidentally, I'm using the Open WATCOM compiler. I have VSC++ installed, but I prefer the simplicity of WATCOM's compiler for these short programs.
    Can anyone tell me whether it is worth using WATCOM. It doesn't seem as if anyone else uses it much.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I personally haven't used WATCOM, but many people here use VSC++/Dev-C++/GCC etc. I suppose WATCOM is C99 compliance support so don’t think you would have much of a problem in terms of portability. But it would still suggest using compiler which many here use and you could expect much more effective help. But it wouldn't make too much of a difference. Its really a personal preference and need of your application. There are few employers out there still using Turbo C cos they need it for their application.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM