Thread: a C structure problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Question 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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Add
    struct Alphabet;
    before Symbol.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    i think that will create similar problem as symbol is not defined at that point, correct me if i am wrong

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Symbol exists; it's a struct, and a struct being defined can refer to itself, although within limitations. So no problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    struct Alphabet;
    
    struct Symbol
    {
        Symbol * E;
        Alphabet * N;  //line 8
    };
    
    struct Alphabet
    {
        char data ;
        Symbol * E;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    ok thanks i get it, (i didn't know that structure can be declared in that way)
    Last edited by bioshock; 02-28-2011 at 01:45 PM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks hk_mp5kpdw for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort structure problem
    By ERJuanca in forum C Programming
    Replies: 1
    Last Post: 02-17-2011, 01:50 PM
  2. Sort structure problem
    By ERJuanca in forum C Programming
    Replies: 1
    Last Post: 02-17-2011, 01:49 PM
  3. poiner to structure problem
    By bluetxxth in forum C Programming
    Replies: 9
    Last Post: 02-25-2010, 04:56 PM
  4. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  5. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM

Tags for this Thread