I am currently trying to complete a Homework Assignment for one of my college courses, and am stuck.
#include <stdio.h>
void add(int *a[], const int *b[], const int *c[], int size); /* Function Prototype */
int main() {
int a[5];
const int b[] = {1,2,3,4,5};
const int c[] = {1,1,1,1,1};
add(a,b,c,5);
for(int i=0; i < 5; i++) {
printf("%d\t", a[i]);
}
return 0;
}
void add(int *a[], const int *b[], const int *c[], int size) {
for (int i = 0; i < size; i++) {
a[i]=b[i]+c[i];
}
}
I am trying to have the function add receive its arguments as pointers instead of as int arrays. I have consulted the textbook, and some of my peers but I am still generally confused. If anyone could please shine some light on why my above implementation does not work?
These are the errors I get:
main.c: In function 'main':
main.c:9:7: warning: passing argument 1 of 'add' from incompatible pointer
type [-Wincompatible-pointer-types]
add(a,b,c,5);
^
main.c:3:6: note: expected 'int **' but argument is of type 'int *'
void add(int *a[], const int *b[], const int *c[], int size); /* Function
Prototype */
^~~
main.c:9:9: warning: passing argument 2 of 'add' from incompatible pointer
type [-Wincompatible-pointer-types]
add(a,b,c,5);
^
main.c:3:6: note: expected 'const int **' but argument is of type 'const int *'
void add(int *a[], const int *b[], const int *c[], int size); /* Function
Prototype */
^~~
main.c:9:11: warning: passing argument 3 of 'add' from incompatible pointer type [-Wincompatible-pointer-types]
add(a,b,c,5);
^
main.c:3:6: note: expected 'const int **' but argument is of type 'const int *'
void add(int *a[], const int *b[], const int *c[], int size); /* Function
Prototype */
^~~
main.c: In function 'add':
main.c:19:14: error: invalid operands to binary + (have 'const int *' and 'const int *')
a[i]=b[i]+c[i];
~~~~^~~~~
exit status 1