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.

8
  • 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? Commented Mar 22, 2014 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. Commented Mar 22, 2014 at 19:34
  • 1
    You should add to question, what output you get, and explain how it is wrong. Commented Mar 22, 2014 at 19:36
  • 1
    arrayPointer[counter] = r; : same values. int r = rand()%(max-min)+min; : range of r is 1 - 48. Commented Mar 22, 2014 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. Commented Mar 22, 2014 at 19:43

2 Answers 2

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]);
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@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.
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 )

4 Comments

...and cast the pointer to void *, because %p expects an argument of that type.
@user3447428 i doubt you should do it explicitly, can you give some reference explaining it ?
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. (In almost every other case, one should not be casting to and from void *, but this situation is exceptional.)
@user3447428 yes, i found this answer. Thanks for teaching me something new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.