1

I'm fairly new to C and am trying to make a dice game. I am trying to learn pointers and need some tips and advice about using call functions and pointers that are initialized in main.

I am getting and error on my first roll call function to determine the first roller:

called object 'first_roll_call' is not a function or function pointer

What does that mean, and how can I fix it?

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main () {
    int dice;
    int roll, first_roll;
    int i;
    int first_roll_call;

    int *playerA;
    int *playerB;
    int *first_rollA;
    int *first_rollB;

    playerA = (int *)malloc(100);
    playerB = (int *)malloc(100);
    first_rollA = (int *)malloc(100);
    first_rollB = (int *)malloc(100);

    printf("Welcome to Dice Mania!\n\n");
    printf("First player to a combined score of 25 wins!\n");
    printf("We to see who goes first, press 1 to continue:");
    scanf("%d", &first_roll);

    for (i = 1; i < 2; i++){
        first_roll = rand() % 6 + 1;
        first_roll_call(first_roll, i, &first_rollA, &first_rollB);
    }
    if (*first_rollA > *first_rollB){
        printf("Player 1 goes first!\n");
    }
    else {
        printf("Player 2 goes first!\n");
    }

    if (dice == 1) {
        while (dice == 1) {
            printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
            srand(time(NULL));
            roll = rand() % 6 + 1;
            printf("You have rolled a: \n\n");
            dice = dice_roll(roll);
            if (dice == 0) {
                printf("\n\nThank you for playing");
                break;
            }
        }
    }

    return 0;
}

int dice_roll(int x) {
    int dice;

    if (x == 1) {
        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -------  *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 2) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*      |    *\n");
        printf("*  ____|    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 3) {
        int dice;

        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*   ___|    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -----    *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 4) {
        int dice;

        printf("*************\n");
        printf("*  |    |   *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*************\n*");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 5) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*       |   *\n");
        printf("*  _____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 6) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }

int first_roll_call(int x, int y, int *first_rollA, int *first_rollB){
    int dice;

    if (x == 1) {
        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -------  *\n");
        printf("*************\n");

        if (y = 1){
            printf("Player one rolled a %d", x);
            x = *first_rollA;

        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
    if (x == 2) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*      |    *\n");
        printf("*  ____|    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if (x == 3) {
        int dice;

        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*   ___|    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -----    *\n");
        printf("*************\n");

        if (y = 1){
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if (x == 4) {
        int dice;

        printf("*************\n");
        printf("*  |    |   *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*************\n*");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
    if (x == 5) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*       |   *\n");
        printf("*  _____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if(x == 6) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
}

}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
msnowboardsdsu
  • 19
  • 1
  • 1
  • 2
  • Just a *nit*, `int main (void)` is the definition for `main` that affirmatively conveys you intended `main` to have no arguments. (don't get me wrong, `int main ()` is fine, too, but you may as well make it as proper as you can) Also, don't cast the return of `malloc`, See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. `playerA = malloc(100 * sizeof *playerA);` is all you need to declare `100 int` (currently you are only declaring `25`, there are `4-bytes` per `int` -- on `x86` and `x86_64`) – David C. Rankin Sep 10 '16 at 06:08
  • Note that there is an incorrectly placed `}`, causing `first_roll_call` to be defined _inside_ the definition of `dice_roll`, so it can't be called from outside that function. Also, function definitions inside each other is a GNU C extension, and not allowed in standard C. – Thomas Padron-McCarthy Sep 10 '16 at 06:22

2 Answers2

6

The compiler is complaining about this line in main():

        first_roll_call(first_roll, i, &first_rollA, &first_rollB);

Its complaint that first_roll_call is not a function is quite correct. Earlier in main() you declared it to be a local variable of type int:

    int first_roll_call;

Inside main(), that declaration shadows any other objects of the same name. If you mean to declare it as a function returning int, then at minimum you need this, instead:

    int first_roll_call();

Really, though, you ought to declare a full prototype for the function:

    int first_roll_call(int, int, int *, int *);

Furthermore, although it is allowed to declare functions inside other functions (but not to nest function bodies!), it is far more conventional to put function declarations at file scope, near the top of the source file.

For a fairly simple case like this, however, moving function main() to the end of the source file will remove any need for it to declare the other functions. The compiler will already have seen the declarations attached to their function bodies by the time it gets to main(), and those would be quite sufficient.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

In C, you have to declare a function before you can use it. The compiler reads your source code strictly from top to bottom.

So either move the whole first_roll_call function to the top of the file, or alternatively write the following line at the top of the file.

int first_roll_call(int x, int y, int *first_rollA, int *first_rollB);

This line declares the function, but leaves its definition for later.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • In fact, that is what you generally see done. You declare a *function prototype* above where it is used, and leave the *function definition* at the end of your code to reduce clutter and improve readability. – David C. Rankin Sep 10 '16 at 06:06
  • No, this is not entirely correct. In C you _can_ call a function before you declare it, and the problem in this case is caused by the local variable with the same name as the function, plus a mistake in nesting causing `first_roll_call` to be defined inside the definition of `dice_roll`. – Thomas Padron-McCarthy Sep 10 '16 at 06:23