Skip to content
Sign upLog in
← Back to Community

I have complied the following script & I am unable 2 understand the error to resolve it

Profile icon
Aravind

Here is the following code :

#include <stdio.h>

struct emp
{
char name [20];
int age;
}
main ()
{
struct emp e = ("Soicher",34);
f(e);
}

f(struct emp ee)
{
printf("\n%s%d",ee.name,ee.age);
}

Voters
Profile icon
Aravind
Comments
hotnewtop
Profile icon
SPQR

When writing your main function (or any function for that matter) you have to initialize it as a type (i.e. int, char, bool). For main I think it has to be an int. What's happening here is, your compiler is interpreting main as a function of type emp, because without a type declaration before main it assumes the
type directly precedes it, which in this case is struct emp.

Essentially what you have is struct emp main which is simultaneously creating an emp struct and creating the main function which returns an object of type emp (which for any other function would be entirely valid BUT main has to be an int.)

Profile icon
KeaganLandfried

@SPQR
whoosh

Profile icon
SPQR
Profile icon
KeaganLandfried

@SPQR
haha I was joking because that looked complicated.

Profile icon
Highwayman

main.c:4:1 warning: ...

For this SPQR is mostly right, he just forgot to add that you will also have to put a ; at the end of you emp definition of the emp type.

main.c:11:15: error: ...

This one can be confusing, but the reason why this doesn’t work it actually quite simple:
your accidentally using the really cool comma operator!
The comma operator evaluates each statement, but only returns the last operation, so what happened is that you attempted to initialize ee to 34. So all you have to do here is instead declare ee without initializing it and then initializing the individual things inside separately.

main.c:12:4: warning: ...

This here is really easy. It’s just basically the same error as before with main on line 4, you forgot to add a return type. So to fix that I would suggest void because that means you are returning nothing which is exactly what f is doing. never never ** NEVER ** make main have a void return type, that is a terrible practice, always return an int.