Link to home
Start Free TrialLog in
Avatar of Tabris42
Tabris42Flag for United States of America

asked on

Confused about pointers

I'm a little unfamiliar with pointers, and badly need some help with them. I have a function here, that creates 300 random 4 digit numbers. I am required to print this array out in another function. (Or in main, it doesn't really matter, but preferrably from another function). Here is the function:

int randomgenerator()
{
     int limit = 1000; int max= 9999; int i; int randentry;
     int randomnumbers[300];
     printf("Enter a seed for random generation: ");
     scanf("%d", &randentry);
     if (randentry == 0){
        randentry == time(0);
     }
     printf("\n");
     srand(randentry);
     for(i = 0;i < 300; i++){
          randomnumbers[i] = (rand() % (max - limit)) + limit;
     }
     for(i = 0;i < 300; i++){
          printf("%d", randomnumbers[i]);
          if (i%14){
              printf(" ");
          }else{
              printf("\n");
          }
     }
      printf("\n");
      system("PAUSE");
      return 0;
}

Obviously the return statement is wrong, but how would I be able to take the array from this function so I can print it, or use it in other functions?
SOLUTION
Avatar of tinchos
tinchos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snehanshu
snehanshu

Tabris42,
  You have done all the hard work, and now need help with the easy part!
:-)
  Recently, I've posted this explanation in many questions and here it is again:
Well, in C, an array is infact a pointer.
  When you declare
int randomnumbers[300];
  What you are saying is that there is a pointer randomnumbers which is assigned 300 continuous memory locations of type integer.
  A pointer contains address of a memory location.
  When you access randomnumbers as randomnumbers[n], you are effectively asking the compiler to deal with the value contained at the address contained in randomnumbers + 0.
  So, that's abput pointers in brief.
  Now, to pass an array to a function, you can simple pass the address of the array's starting location.
  That means, your function should accept a pointer as follows:
  int mydisplay( int *myarray)
  {
    //use myarray[0] to myarray[299] for displaying
  }
  Now, when you call mydisplay from main like
mydisplay(randomnumbers);

Hope that explained something.
...Snehanshu
However, I would rather have the array created and released in the same function, so I would do like.......

void randomgenerator( int * randomnumbers, int cantElements )
{
   int limit = 1000; int max= 9999; int i; int randentry;
   printf("Enter a seed for random generation: ");
   scanf("%d", &randentry);
   if (randentry == 0){
      randentry == time(0);
   }
   printf("\n");
   srand(randentry);
   for(i = 0;i < cantElements; randomnumbers++, i++){
        *randomnumbers = (rand() % (max - limit)) + limit;
   }
   for(i = 0;i < cantElements; randomnumbers++, i++){
        printf("%d", *randomnumbers );
        if (i%14){
            printf(" ");
        }else{
            printf("\n");
        }
   }
    printf("\n");
    system("PAUSE");
}

and then you'll use it like this

// Code

int vector[300];

randomgenerator( vector, 300 );

for( int i = 0; i < 300; i++ )¨
    printf( "%d ", *vector );


// Or

int *vector = (int *)malloc( 300 * sizeof( int ) );

randomgenerator( vector, 300 );

for( int i = 0; i < 300; i++ )¨
    printf( "%d ", *vector );

free( vector );
OK, I misunderstood the question. Ignore my comment and go ahead with tinchos explanation.
...Snehanshu
Hi Tabris,

An array is just a pointer to the first item in the array. So if you return randomnumbers (or randomnumbers[0] which is the same), you can then use it later on. I would do something like this:

void randomgenerator( int* randomnumbers ) {
    ...your code...
     for(i = 0;i < 300; i++){
          randomnumbers[i] = (rand() % (max - limit)) + limit;
     }
    ...more code...
}

int main() {
    int randomnumbers[300];
    int i;

    randomgenerator( randomnumbers );
    for(i = 0;i < 300; i++){
          printf("%d", randomnumbers[i]);
          if (i%14){
              printf(" ");
          }else{
              printf("\n");
          }
    }
}

Cheers,
Arjan
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tabris42

ASKER

Guys, your answers are very helpful, but the code you're giving me totally screws up the numbers I'm supposed to be getting.
The problem is the following

When you assign the values to the vector, then, you continue printing the pointer from the end of the vector, so you're printing places where the data has not been assigned..........


    for(i = 0;i < cantElements; randomnumbers++, i++){
         *randomnumbers = (rand() % (max - limit)) + limit;
    }
////////////////////////////////////////////////////////////////////////////////////
Here, as you don't reassign randomnumbers, you will print all the values stored after the end of the data stored
////////////////////////////////////////////////////////////////////////////////////

    for(i = 0;i < cantElements; randomnumbers++, i++){
         printf("%d", *randomnumbers );
         if (i%14){
             printf(" ");
         }else{
             printf("\n");
         }
    }

Do as the following

void randomgenerator( int * randomnumbers, int cantElements )
{
   int limit = 1000; int max= 9999; int i; int randentry;
   int * tempPointer;

   printf("Enter a seed for random generation: ");
   scanf("%d", &randentry);

   if (randentry == 0){
      randentry = time(0);
   }
   printf("\n");
   srand(randentry);

   // Assign the pointer to use with the original origin of the vector
   tempPointer = randomnumbers;
   for(i = 0;i < cantElements; tempPointer++, i++){
        *tempPointer = (rand() % (max - limit)) + limit;
   }

   // Reassign the pointer to use with the original origin of the vector
   tempPointer = randomnumbers;
   for(i = 0;i < cantElements; tempPointer++, i++){
        printf("%d", *tempPointer );
        if (i%14){
            printf(" ");
        }else{
            printf("\n");
        }
   }
    printf("\n");
    system("PAUSE");
}

I tested this and works just fine

Cheers

Tincho