-
a C structure problem
hi
i need two structure which is basically like this
Code:
struct Symbol {
Symbol * E;
Alphabet * N; //line 8
};
typedef struct Alphabet {
char data ;
Symbol * E;
};
i get a error : LINE :8 ISO C++ forbids Declaration Of 'Alphabet' With No Type
so i guess alphabet type is not defined yet i mean at that point, how do i overcome this problem
-
Add
struct Alphabet;
before Symbol.
-
i think that will create similar problem as symbol is not defined at that point, correct me if i am wrong
-
Symbol exists; it's a struct, and a struct being defined can refer to itself, although within limitations. So no problems.
-
its not working
thanks for you quick reply
but its not working for me
Code:
typedef struct Alphabet {
char data ;
Symbol * E; //shows error: ISO C++ forbids Declaration Of 'Symbol' With No Type
};
struct Symbol {
Symbol * E;
Alphabet * N; //line 8
};
i am using DEV C++ With GCC Compiler
-
Code:
struct Alphabet;
struct Symbol
{
Symbol * E;
Alphabet * N; //line 8
};
struct Alphabet
{
char data ;
Symbol * E;
};
-
ok thanks i get it, (i didn't know that structure can be declared in that way)
-
It's called a forward declaration. It tells the compiler "hey there is this struct called Alphabet but you don't need to know the details just yet." It allows the compiler to parse through that Symbol struct without causing issues like you are getting because now the compiler knows in general what it is (a struct) even if it does not know exactly what it looks like in detail.
-
thanks hk_mp5kpdw for the explanation