starlight starlight - 28 days ago 10
C Question

Structs and Functions in C (Parameters) -- struct undeclared?

The programming is receiving an error saying that p3 is undeclared and that is is the first use in this function on line 26. I am doing some coding practice for a test I have to take tomorrow. I thought struct p3; would be declaring p3? Help!

#include <stdio.h>

struct records{
float SSN;
float wage;
};

int agree(struct records p1, struct records p2, struct records *final );

int main(){
struct records p1;
int flag;

p1.SSN = 12;
p1.wage = 120;

struct records p2;
p2.SSN = 12;
p2.wage = 120;

struct p3;

flag = agree(p1, p2, &p3);

return 0;
}

int agree(struct records p1, struct records p2, struct records *final){

if(p1.SSN == p2.SSN){
// final->SSN = p1.SSN;
// final->wage = p1.wage + p2.wage;
return 1;
}

else {
return 0;
}

}

Answer

In your code

  struct p3;

should be

 struct records p3;

for p3 to be a variable of type struct records. Otherwise, a declaration like struct p3; is a type declaration for which no type definition is there, which is the cause of the error.