Thread: pointer issue

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    pointer issue

    I'm writting a program that generates a list of random numbers based on predefined parameters and counts the number of positives, negatives, zeros, evens and odds. I had it working fine when it was all in main() but when I broke it up into separate functions it would not display the number of positives etc. correctly.

    Here is the code for the part that I believe is giving me trouble:
    Code:
    int looper(int seed, int upper, int lower, int num, int* pos, int* neg, int* zero,
              int* even, int* odd)
    {
       int rnum, teh;      //rnum is random number, teh counts the loops
        
       srand(seed);
       for (teh=0; teh<num; teh++) //loop controles # of randoms
       {
        rnum= (rand()%(upper-lower))+lower;
        if(teh==10 || teh==20 || teh==30 || teh==40 || teh==50)     //new line every 10 loops
         { printf("\n"); }
        printf(" %d", rnum);     //print the random
    
        if(rnum>0)       //counts positives, neg, zero, even & odds
         { pos++; }
        else if(rnum<0)
         { neg++; }
        else
         { zero++; }
    
        if((rnum%2)==0)
        { even++; }
        else
        { odd++; }
       }
       printf("\n");
    }
    That string of if/else statements at the end should be counting the number of evens, odds etc. but all I get for the values is 0 when I compile and run the program. I've looked through the FAQs and my textbook but nothing has clued me in to why it isn't working the way I want.
    Thanks in advance for any help.
    Last edited by Justinb; 10-17-2010 at 07:34 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In this function, what is a variable like "pos"?

    int, double char, float, long int, unsigned char?

    No - it's a pointer!! And a pointer holds an address value. And you are incrementing THAT ADDRESS value -- which is not what you want.

    Try:
    Code:
    (*pos)++; //yes, you need the parentheses

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thanks, that worked perfectly. I had tried using *pos but didn't use the parentheses.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yep! Figured as much - we've all been stung by that precedence bee, at some time or other.

    And Welcome to the forum, Justinb!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with pointer issue
    By ph5 in forum C Programming
    Replies: 4
    Last Post: 11-17-2009, 04:19 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM