Thread: Structure help.

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Structure help.

    Basically, I'm trying to define a structure type and I'm getting a syntax error on the third to last line.
    Code:
    struct term
    {
     char sign;
     char coef;
     char xvar;
     char yvar;
     char oparen;
     term pterm[5]; //error here
     char cparen;
    };
    Also, on the same line I'm getting,
    [Warning] no semicolon at the end of struct or union
    I read in the tutorial on structures that the syntax for defining a structure type is different in C than C++, but when I comment out that line, it compiles fine. And, I know you can use a variable whose type is the structure you are defining, because it was done in the article on the Huffman compression technique.

    Any thoughts?

    BTW, I'm using the DevC++ compiler.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Replace:
    Code:
    term pterm[5]; //error here
    with:
    Code:
    struct term pterm[5]; // no error now
    If you understand what you're doing, you're not learning anything.

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    Code:
    struct term
    {
     char sign;
     char coef;
     char xvar;
     char yvar;
     char oparen;
     term pterm[5]; <--------
    };
    <---- The compiler doesn't know the storage requirement for struct term. So it will flag an error.
    The only way around this is,

    Code:
    struct term
    {
     char sign;
     char coef;
     char xvar;
     char yvar;
     char oparen;
     struct term* pterm;  
    };
    This works because, the size of a pointer type is constant ( for that particular OS and architecture ) and known before by the compiler.

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Bah. Of course. Thankies.

    Ah, nevermind. More errors. On that same line,
    Code:
    struct term pterm[5];
    I'm now getting the error message,
    field 'pterm' has incomplete type
    But, wait! I changed the variable to a pointer and it works fine. Thanks.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itsme86
    Replace:
    Code:
    term pterm[5]; //error here
    with:
    Code:
    struct term pterm[5]; // no error now
    Yeah except you can't do that. You can't indefinately have one structure contain an actual instance of itself. You can have it store pointers to instances of itself, but you can't have it store actual instances. Why? Well, what happens if you have a recursive function which never ends? Exactly. That's what would happen if you had a structure contain an instance of itself.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM