2

So for some Uni work I need to create an array using a function (my first time with C functions and pointers) but store the array as a pointer because i dont think C can use arrays in functions? And then also use another function to print out each element in the array. The code i use in main is:

    int* x = get_lotto_draw();
    print_array(x);

And then my functions are:

int* get_lotto_draw() //Returns an array of six random lottery numbers 1-49
{
     int min = 1;
     int max = 49;
     int counter = 0;

     srand(time(NULL));
     int r = rand()%(max-min)+min;

     int *arrayPointer = malloc(6 * sizeof(int));

     for(counter = 0; counter <= 5; counter++)
     {
                 arrayPointer[counter] = r;
     }  

     return arrayPointer;
}



void print_array(int * array) //Print out the content of an array
{
     int i = 0;
     int printerArray[6] = {0, 0, 0, 0, 0, 0};

     for(i = 0; i <= 5; i++)
     {
           printerArray[i] = array[i];
     }

     printf("array = %d", array);
     printf("printerArray = %d", printerArray);

     for(i = 0; i <= 5; i++)
     {
           printf("Array element %d : %d\n", i, printerArray[i]);
     }
}

But im doing something wrong, and either the array isnt getting created correctly, or the print isnt working correctly. Thanks for your time.

hjalpmig
  • 702
  • 1
  • 13
  • 39
  • You could certainly pass arrays around similar to the way pointers are, but more importantly what error messages or erroneous output are you getting that indicates that your code is problematic? – ciphermagi Mar 22 '14 at 19:30
  • @self. Ah, scrolling mistake in my part. To OP, you might want to use `%p` format specifier to print pointers, instead of `%d`. – hyde Mar 22 '14 at 19:34
  • 1
    You should add to question, what output you get, and explain how it is wrong. – hyde Mar 22 '14 at 19:36
  • 1
    `arrayPointer[counter] = r;` : same values. `int r = rand()%(max-min)+min;` : range of `r` is 1 - 48. – BLUEPIXY Mar 22 '14 at 19:37
  • 2
    I have compiled and ran that code with no problems, except that `get_lotto_draw` generates array of six equal elements instead of six random elements. If you want each element to be different, you should call `rand()` inside the loop where array elements initialize. – Oleg Andriyanov Mar 22 '14 at 19:43
  • This code doesn't have anything wrong in particular. **What** do you think is "wrong"? Just stating that "it's not correct" is not helfpul and just wastes our time (and yours). – user3447428 Mar 22 '14 at 20:10
  • Plus, it's more idiomatic to write `i < 6` instead of `i <= 5` in the condition of the for loop. – user3447428 Mar 22 '14 at 20:11
  • Thanks all for your answers. Sorry i was not specific, it was that all values were being printed the same but @Oleg Andriyanov 's answer has fixed this problem, I should have used "rand()%(max-min)+min" in the loop rather than before it. Now thats fixed, what would the easiest way be to go about checking if any of the pointer values are equal to eachother? (so that i can replace with another value, as all pointer values must be unique) Im new to programming, sorry if this is an obvious question. – hjalpmig Mar 22 '14 at 20:36

2 Answers2

1

Following two lines could provoke undefined behavior

 printf("array = %d", array);
 printf("printerArray = %d", printerArray);

you can't use %d here, as array and printerArray decays to pointers in this context and in order to print pointer you should use %p and cast your arrays to void * (thanks to @user3447428 for his comment about cast )

Dabo
  • 2,371
  • 2
  • 18
  • 26
  • ...and cast the pointer to `void *`, because `%p` expects an argument of that type. – user3447428 Mar 22 '14 at 20:09
  • @user3447428 i doubt you should do it explicitly, can you give some reference explaining it ? – Dabo Mar 22 '14 at 20:18
  • you can doubt it, but it **should** be done, since `printf()` is variadic, so all sort of weird promotions can apply to pointers, which will quickly cause undefined behavior if you don't include the cast. [See this](http://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-address). (In almost every other case, one should **not** be casting to and from `void *`, but this situation is exceptional.) – user3447428 Mar 22 '14 at 20:23
  • @user3447428 yes, i found [this](http://stackoverflow.com/a/20435076/2549281) answer. Thanks for teaching me something new. – Dabo Mar 22 '14 at 20:32
1

What you want is:

void print_array(int * array) //Print out the content of an array
{
     int i = 0;

     for(i = 0; i <= 5; i++)
     {
           printf("Array element %d : %d\n", i, array[i]);
     }
}
mah
  • 39,056
  • 9
  • 76
  • 93
  • @vonbrand (editor) -- nothing wrong with the cleanup you edited, but it wasn't necessary and I wanted the function to resemble the OP's function as closely as possible since I just edited his function to solve his problem. For that reason I rolled back your edit. – mah Mar 24 '14 at 19:25