Reputation: 5940
I wrote a simple C program because I just got started learning how to program. Here is the main.c file:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
float mul(float r[],float o[]){
o[0] = r[0]*11;
o[1] = r[1]*22;
o[2] = r[2]*33;
}
//==============================================================
int main(void){
float r[3];
r[0]=1;r[1]=2;r[2]=3;
float o[3];
o=mul(r,o);
return 0;
}`
My goal is to fill vector o by using the function mul; I am required to keep the definition of the variables as it is; the only thing that I am supposed to modify is the function.
When I run it by typing gcc main.c
I get an error message like this:
error: incompatible types when assigning to type ‘float[3]’ from type ‘float’
o=mul(r,o);
And I have no Idea how to fix it. What am I supposed to change in my little code? as far as I learned I should pass to the function the pointers referred to the vectors but it should be the same thing of what I have done since the vector name is the pointer to the vector itself.
Thanks for your help
Upvotes: 0
Views: 273
Reputation: 13171
o=mul(r,o);
assigns o
to the return value of the mul
function. But mul
has no return value--it has no return statement at all, nor does it need one, because it operates directly on the arrays passed to it. Instead, just declare it void
:
void mul(float r[], float o[]) {
. . .
Then call it without the assignment:
mul(r, o);
Upvotes: 0
Reputation: 225032
In C it's called an "array", not a "vector".
You can't assign a value to an array.
You have mul
declared as returning a float
, but it doesn't return anything - change that to void
.
When you call mul
, since it doesn't return anything, you don't need the o=
. Just mul(r,o)
, will be fine.
An array is not a pointer. That said, an array does decay into a pointer to its first element in most contexts, including the function call you're using it in. Likewise, the float r[], float o[]
in your mul
function signature is just syntactic sugar for float *r, float *o
.
Upvotes: 2