Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Malloc Array, trouble printing in new function in C

Hi I’ve created an array and used malloc to store it so it could use it in a different function but now I’m running into problems with printing the array as it says:

warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
             printf("1-ere %d -- \n", roll_dice_outcome);

Help please 😀

int *roll_multiple_dice(int N){
    int i;
    int *roll_dice_outcome = malloc(sizeof(int) * N);

        for (i = 0; i < N; i++)
        { 
            roll_dice_outcome[i] = (rand() % 6) + 1;
            printf("%d ", roll_dice_outcome[i]);
        }

    return roll_dice_outcome;
}

void play_yatzy(int i, int *roll_dice_outcome){
    
    
        switch (i)
        {
        case 1:
            printf("1-ere %d -- \n", roll_dice_outcome);
            break;
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cant print the array this way.

You need to iterate through all elements and print them individually:

void printResults(const int *results, size_t size)
{
    for(size_t i = 0; i < size; i++)
    {
        printf("Result no %zu = %d\n", size, results[i]);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading