Search code examples
cfunctioncompiler-errorsstructure

C Structures Definition inside Function


I was trying to make structure variable inside a function returning structure. Now for some reason, I am not able to instead it shows an error.

#include<stdio.h>

struct Vector{
    int x;
    int y;
};

struct Vector Sum_Vector(struct Vector v1, struct Vector v2){
    struct vector v;
    v.x = v1.x + v2.x;
    v.y = v1.y + v2.y;
    return v;
}

int main(){
    struct Vector v1,v2,v3;
    v1.x = 10;
    v1.y = 11;

    v2.x = 5;
    v2.y = 6;

    v3 = Sum_Vector(v1,v2);
    printf("Vector: %di+%dj\n",v3.x,v3.y);
    return 0;
}

Error Message:

test.c: In function 'Sum_Vector':

test.c:9:19: error: storage size of 'v' isn't known

9 |     struct vector v;

  |

Why This error is coming? Is it because we cannot create a structure variable inside function or something else?


Solution

  • You declare

    struct Vector{
        int x;
        int y;
    };
    

    But you write:

    struct vector v;
    

    The language is case sensitive: struct vector is not defined while struct Vector is.