C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Yes / No Questions (Q.No. 10)
10.
Will the following declaration work?
typedef struct s
{
    int a;
    float b;
}s;
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Himanshu said:   3 years ago
Yes, you are right @Rishi.

Ajayv said:   7 years ago
How to access members of this anonymous structure @Shrinivas Patgar.

Zdd said:   7 years ago
I agree @Wikiok @Rishi.

Rishi said:   9 years ago
Yes, it will work.

The reason is that typedef is used to assign an alternative name to the user to define data types as well.

So, here typedef creates the name "s" (structure) which can be used again in the program.

Prince Bansal said:   1 decade ago
Try this one:

int main()
{
struct s
{
int a;
float b;
};
struct s s;
s.a=10;
printf("%d",s.a);
}

This is working.
Can anyone explain it?

Om prakash said:   1 decade ago
Yes I agree with @Shrinivas patgar. We use typedef for anonymous structure (no name for the structure).

Shrinivas Patgar said:   1 decade ago
I think it will not work instead of this.

typedef struct
{ int a ;
float b;
}s;

will work.

Gopi said:   1 decade ago
It will not work because structure variable name should not be the same as structure name.

Wikiok said:   1 decade ago
typedef long int LI;
int main ()
{
LI LI; //Works, no error!!!
LI=10;
}
So defined type's name can be used as a variable name.

Chinna said:   1 decade ago
If any problem using same structure variable like structure name.

Please explain it.

Post your comments here:

Your comments will be displayed after verification.