Quote:
Originally Posted by yousafsajjad
this is strange
|
It was a surprise to me too.
I didn't know that C++ (g++ anyway) lets you repeat a typedef within one compilation unit.
You can't repeat most other definitions within a compilation unit even if the two definitions are identical.
Strip away all the irrelevant complexity of your example. The following code works in c++ and fails in c
Code:
typedef int Int;
typedef int Int;
Quote:
i did use forward declaration in c++ but in c it is giving me an error
|
You are mixing a typedef with a forward declaration. The typedef itself is not a forward declaration.
Code:
typedef struct Student S;
In both C and C++ that is a forward declaration of
struct Student combined with a typedef of
S.
It is not a forward declaration of S.
Code:
typedef struct Student{
char name[100];
}S;
That is both a definition of
struct Student and a redundant second typedef of
S.
I don't know why that is correct in C++.
In either C or C++, the extra typedef serves no useful purpose. I think the code you want is
Code:
typedef struct Student S;
struct Student{
char name[100];
};
That should correctly define S in either C or C++.
For any lurkers who wonder why someone would do any of that in the first place (vs. combine the typedef and struct declaration without any forward declaration), typically the programmer would be working toward something like
Code:
typedef struct Student S;
struct Student{
char name[100];
S* next;
};
So (in C) S must be declared before Student is defined.