0

C newbie here. Trying to figure out the error in my program.

Function prototype:

float* convolve(int (*)[10], int (*)[3], int *, int);

actual function:

float* convolve(int* ImdataPtr, int* KernelPtr, int* size, int sizeKernel)

How it's called in main:

float* output;
output = convolve(input,kernel,sizeIm,3);

Compile Error:

program.c:55:8: error: conflicting types for ‘convolve’

Help, please...

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
nineties
  • 423
  • 1
  • 7
  • 17
  • What is `input`, `kernel` and `sizeIm`? Please include the declarations here. – ruben2020 Jun 19 '13 at 19:27
  • 2
    Look at the error message: it starts off `program.c:55:8`. That tells you (If I interpreted it right) that (a) the problem is in the `program.c` file, (b) the problem is on like 55, and (c) that the problem is at character 8 of that line. Error messages are actually helpful in finding problems - don't just dismiss them as your compiler whining. – AJMansfield Jun 19 '13 at 19:30
  • @AjMansfield: That is not helpful. The OP already identified the lines causing the error. They understand the location of the problem. The question is about the reason. – Eric Postpischil Jun 19 '13 at 19:43

2 Answers2

6

The problem is that the prototype doesn't match. Make sure the types are exactly the same, since int(*)[10] and int(*)[3] are different types than int*:

float* convolve(int(*)[10], int(*)[3], int*, int);

float* convolve(int (*ImdataPtr)[10], int (*KernelPtr)[3], int* size, int sizeKernel) {
    // etc
}

You can (and probably should) even make them exactly the same, including argument names:

float* convolve(int (*ImdataPtr)[10], int (*KernelPtr)[3], int* size, int sizeKernel);

float* convolve(int (*ImdataPtr)[10], int (*KernelPtr)[3], int* size, int sizeKernel) {
    // etc
}

I had to look up how to declare these, so you might find the question on C pointer to array/array of pointers disambiguation useful too. int*[3] is an array of pointers to int (just read it backwards), but int(*)[3] is a pointer to an array of int.

Community
  • 1
  • 1
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • 2
    There is no harm in putting argument names in your prototypes. It's a very good thing for other people who are consuming your header file so they can get a better idea of your intent. – plinth Jun 19 '13 at 19:29
  • The arguments are matrix pointers, and i declare them inside main. Well, i guess i can define them before the prototype... Thanks! – nineties Jun 19 '13 at 19:32
  • @htann1900 The actual type doesn't matter, what's important is that they match. If you declare an argument as `int**` in the prototype, it needs to be an `int**` when you implement the function. – Brendan Long Jun 19 '13 at 19:34
  • @BrendanLong: The prototype does not have parameters of type `int **`. It has parameters of type `int (*)[10]` and `int (*)[3]`, which are different. The former are pointers to pointers. The latter are pointers to arrays. – Eric Postpischil Jun 19 '13 at 19:44
  • The problem is still there after I change the code. In main, I declare: three array and pass their name as convolve(ar1,ar2,ar3,sizeKernel). And I get the message, 'note: expect 'int *', but argument is of type 'int (*)[3]' – nineties Jun 19 '13 at 19:45
  • expect 'int asterisk', but... type 'int (asterisk)[3]' – nineties Jun 19 '13 at 19:49
  • @EricPostpischil You're right, C's handling of pointers/arrays still confuses me. – Brendan Long Jun 19 '13 at 19:50
  • @BrendanLong: `int *[3]` is an array of three pointers to `int`. For a pointer to an array of three `int`, you need `int (*)[3]`. – Eric Postpischil Jun 19 '13 at 19:52
  • @EricPostpischil Thanks. I actually had to look up some references to figure out how to define parameters like that with a name.. – Brendan Long Jun 19 '13 at 20:02
  • @htann1900 This version should work, assuming your prototype was correct. If it's not, we'd need to know the actual types of the things you're passing to this function. – Brendan Long Jun 19 '13 at 20:08
  • @BrendanLong Yep, that does it. Thanks a lot :) – nineties Jun 19 '13 at 20:11
1

Your prototype specifies, for its first and second arguments, pointer to an array of integers , whereas in your function, you are specifying integer pointers alone.

You need to either correct your prototype, or your function definition.

Sagar
  • 9,456
  • 6
  • 54
  • 96