CMOS_BATTERY
CMOS_BATTERY

Reputation: 37

Implicit declaration of function with structs

I am trying to fix my last implicit declaration of a function by creating the prototype function in my header file. The lab is to write structs and currently I have my Main.c, RandomNums.h, and RandomNums.c. My function in RandomNums.c is finished but I'm not sure how I should write it in RandomNums.h

Below is what I have for my RandomNums.c file and the issue is that setRandomVals is an implicit declaration in Main.c

struct RandomNums setRandomVals(int low,int high) {
    struct RandomNums r;
    r.var1= (rand() % (high -low + 1)) + low;
    r.var2= (rand() % (high -low + 1)) + low;
    r.var3= (rand() % (high -low + 1)) + low;
     
    return r;
}

Below is how I called SetRandomVals

RandomNums r = SetRandomVals(low, high);

The prototype I had tried was

setRandomVals(int low,int high); 

As for my professor, I may not edit the Main.c file.

Upvotes: -2

Views: 649

Answers (2)

ikegami
ikegami

Reputation: 385657

The return type needs to be included in the declaration.

And to do that, you will need to include the struct definition in the .h.

struct RandomNums { ... };

struct RandomNums setRandomVals(int low, int high);

By the way, you can avoid using the word struct everywhere by using a (possibly-anonymous) struct.

typedef struct { ... } RandomNums;

RandomNums setRandomVals(int low, int high);

Upvotes: 0

Alaa Mahran
Alaa Mahran

Reputation: 663

The prototype shall be the same as the function header in the definition: struct RandomNums setRandomVals(int low,int high).

and it should be declared before any caller calls the function.

Upvotes: 0

Related Questions