0

I'm trying to make a array of structures which contain a string and a function pointer, however when I compile I get a warning that I've initialized from an incompatible pointer type.

I have no idea why (sorry if I sound ignorant, I'm fairly new to C programming).

 typedef struct
{
    char Player1[2], Player[2], **gameGrid;
    int height,width;
    int moveNum, player1Num, player2Num;
    bool player1Win, player2Win;
}Game;

typedef int (*pointer_func)(Game *);

typedef struct
{
   char *funcName;
   pointer_func *f;
}userFunc;

int save(Game *struc);
int load(Game *struc);
int move(Game *struc);
int quit(Game *struc);
void free_grid(Game *struc);


int main(){
//initialised variables 

userFunc Name_arr[] = {
    {"save",save},
    {"load",load},
    {"quit",quit},
    {"move",move}
};

The four functions being referenced are as follows:

int save(Game *struc)
{
char *str, *inputString, *writeString;
FILE *fp;
int nextPlayer;
int maxRead = 20;
bool DIRresponse;

while(true)
{
    printf("Please provide a file name (20 characters max): ");
    inputString = input_String(inputString, maxRead, stdin);

    if((DIRresponse = check_Directory(inputString)) == true){
        printf("That name already exists, choose another\n");
        continue;
    }
    else
        break;  
}

if(struc->moveNum % 2 == 0)
    nextPlayer = struc->player1Num;
else
    nextPlayer = struc->player2Num;

sprintf(str,"%s.txt",inputString);
fp = fopen(str,"w");

sprintf(writeString, "%d %d %d %d %d",  nextPlayer, struc->height,
            struc->width, struc->moveNum, struc->moveNum);
fprintf(fp,writeString);

fclose(fp);

return 0;
}

int move(Game *struc)
{
 return 1;
}

int load(Game *struc)
{
 return 1;
}

int quit(Game *struc)
{
 free_grid(struc);
 exit(EXIT_SUCCESS);
}
6
  • 1
    Please make your minimal reproducible example more minimal. Commented Jan 26, 2018 at 7:21
  • 2
    Please post the exact text of the compiler warning, and if needed, indicate where the line is in your code. Commented Jan 26, 2018 at 7:23
  • try to explicity cast and set values : userFunc Name_arr[] = { (userFunc){ .funcName = "save", .f = save} /* other array items */ }; Commented Jan 26, 2018 at 7:25
  • 2
    pointer_func is already a pointer, you have pointer to pointer to function initialized with pointer to functions. Commented Jan 26, 2018 at 7:26
  • BTW, you don't allocate any space for str or write_string, and the sprintf can be merged with the fprintf. Commented Jan 26, 2018 at 7:28

1 Answer 1

4

You have a mismatch in levels of pointers:

typedef int (*pointer_func)(Game *); << Pointer type

typedef struct
{
  char *funcName;
  pointer_func *f;              << Pointer to a pointer type.... OOPS
}userFunc;

Make *f -> f and it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh, I always feel so stupid after being shown what I've overlooked, regardless thank you for the swift and elegant response.
Good. Then you will remember it and don't do it again. ;)

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.