Goudam M
Goudam M

Reputation: 19

warning occurs in c for struct using callback function

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

typedef struct complex {
    int real;
    int imag;

} complex;
complex n1, n2;
complex add(complex n1, complex n2) {
    complex temp;
    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;
    printf("%d+%di",temp.real,temp.imag);
    return (temp);
  
}
complex sub(complex n1,complex n2)
{
    complex temp;
    temp.real = n1.real - n2.real;
    temp.imag = n1.imag - n2.imag;
    printf("%d+%di",temp.real,temp.imag);
    return (temp);
  
}
complex mul(complex n1,complex n2)
{
    complex temp;
    temp.real = (n1.real * n2.real)-(n1.imag * n2.imag);
    temp.imag = (n1.real*n2.imag)+(n1.imag * n2.real);
    printf("%d+%di",temp.real,temp.imag);
    return (temp);
  
}
void c(complex n1,complex n2,void(*ptr)(complex,complex))
{
  (*ptr)(n1,n2);
}
int main() 
{
  
  int choice;
  printf("For 1st complex number \n");
  printf("Enter the real and imaginary parts: ");
  scanf("%d %d", &n1.real, &n1.imag);
  printf("\nFor 2nd complex number \n");
  printf("Enter the real and imaginary parts: ");
  scanf("%d %d", &n2.real, &n2.imag);
  printf("which operation  can do:");
  scanf("%d",&choice);
  switch(choice)
  {
    case 1:
    c(n1,n2,&add);
    break;
    case 2:
    c(n1,n2,&sub);
    break;
    case 3:
    c(n1,n2,&mul);
    break;
    default:
    printf("bye enter the choice btw 1-3");
    break;
  }
    
    return 0;
}

This is the warning from the compiler:

warning: incompatible function pointer types passing 'complex (*)(complex, complex)' 
(aka 'struct complex (*)(struct complex, struct complex)') to parameter of type 
'void (*)(complex, complex)' (aka 'void (*)(struct complex, struct complex)')
[-Wincompatible-function-pointer-types]

Upvotes: 1

Views: 113

Answers (1)

Jan
Jan

Reputation: 2342

Meaning of the warning

The warning message is actually pretty straight forward:

It means the function that receives your complex-function-pointer here:

void c(complex n1,complex n2,void(*ptr)(complex,complex))
{
  (*ptr)(n1,n2);
}

does not accept function-pointers of type complex(*)(complex, complex) but of type void(*)(complex, complex). So it expects a pointer to a function that returns nothing (void) but it gets a function pointer to a function that returns complex.

Also have a look at a bit more insight into function pointers and their meanings here: How do function pointers in C work?

Solution

You have two options to change your program:

Either you change the argument to your function that receives the function pointers from void(*ptr) ... to complex(*ptr) ... (Option 2 below) or you change the signature of the functions that are used from complex(*ptr) ... to void(*ptr) ... (Option 1 below).

Option 1 (Preferred): Change all producers (functions you are pointing to)

For all complex-number functions change the implementations analogously to this:

void add(complex n1, complex n2) {
    complex temp;
    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;
    printf("%d+%di",temp.real,temp.imag);

    // omitting the return-value here
    // you could also explicitly return like so:
    // return;
}

In my opinion this is the preferred solution, because right now you don't use the return value of the functions.

Option 2: Change consumer (receiver of function pointer)

void c(complex n1,complex n2,complex(*ptr)(complex,complex))
{
  (*ptr)(n1,n2);
}

Background

Just to clarify the meaning of the function-pointer-notion:

void(*ptr)(complex, complex) is referring to a function-pointer named ptr, that points to a function which receives two values of type complex and returns void (nothing).

complex(*ptr)(complex, complex) is referring to a function-pointer named ptr, that points to a function which receives two values of type complex and returns a value of type complex.

Upvotes: 3

Related Questions