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;
}
}
}
}
int main (void)is the definition formainthat affirmatively conveys you intendedmainto 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 ofmalloc, See: Do I cast the result of malloc? for thorough explanation.playerA = malloc(100 * sizeof *playerA);is all you need to declare100 int(currently you are only declaring25, there are4-bytesperint-- onx86andx86_64)}, causingfirst_roll_callto be defined inside the definition ofdice_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.