Thread: Correct way to initialize a typedef strcuture in compile time (with code)

  1. #1
    Registered User vinians's Avatar
    Join Date
    Mar 2015
    Posts
    5

    Correct way to initialize a typedef strcuture in compile time (with code)

    Code:
    typedef struct token 
    {
        int        tokenType;      // what token is that
        int        tokenCode;         // the code of a function if applicable
        char    *tokenString;    // Source token
        double     tokenValue;        // if token is a number
    } TToken;
    
    TToken *tokenTab[] = {TK_COMMAND, 001, "PRINT", 0.0,
                          TK_NUMFUNC, 100, "SIN", 0.0,
                          TK_NUMFUNC, 101, "COS", 0.0,
                          TK_NUMFUNC, 102, "TAN", 0.0,
                          TK_NUMFUNC, 103, "CDBL",0.0,
                          TK_NUMFUNC, 104, "LEN", 0.0,
                          TK_STRFUNC, 200, "LEFT", 0.0,
                          TK_STRFUNC, 200, "MID", 0.0,
                          TK_STRFUNC, 200, "RIGHT",0.0,
                          0, 0, NULL, 0.0};
    Errors:
    Correct way to initialize a typedef strcuture in compile time (with code)-errors-jpg

    I got several warnings and erros, is it possible to declare a table like that ? What's the correct way to declare it ?
    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You declared tokenTab to be an array of pointers to TToken. Problem is, it looks like you are trying to initialise it as an array of TToken objects. So, you either need to change the type or change the initialisation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User vinians's Avatar
    Join Date
    Mar 2015
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You declared tokenTab to be an array of pointers to TToken. Problem is, it looks like you are trying to initialise it as an array of TToken objects. So, you either need to change the type or change the initialisation.
    HUmm, I changed it to
    Code:
    TToken tokenTable[] = {TK_COMMAND, 001, "PRINT", 0.0,
                          TK_NUMFUNC, 100, "SIN",     0.0,
                          TK_NUMFUNC, 101, "COS",     0.0,
                          TK_NUMFUNC, 102, "TAN",     0.0,
                          TK_NUMFUNC, 103, "CDBL",    0.0,
                          TK_NUMFUNC, 104, "LEN",     0.0,
                          TK_STRFUNC, 200, "LEFT",     0.0,
                          TK_STRFUNC, 201, "MID",     0.0,
                          TK_STRFUNC, 202, "RIGHT",    0.0,
                          0, 0, NULL, 0.0};
    and it seems to work (perhaps it compiles) but I'm trying to understand why it does not work with use of TToken *tokenTable[], that is, using a vector of pointers...
    thank you!
    Last edited by vinians; 03-22-2015 at 12:47 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It does not work when you declare an array of pointers because the aggregate that you are using for the initialisation simply does not correspond to an array of pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User vinians's Avatar
    Join Date
    Mar 2015
    Posts
    5
    Yeah, its true, I read some papers about it and now I figured it out. Thank you!
    I tryed to change the title to "SOLVED" but I found no button to change the title.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2013, 10:11 AM
  2. Replies: 1
    Last Post: 05-14-2011, 02:55 PM
  3. Replies: 3
    Last Post: 11-20-2008, 12:31 PM
  4. Compile time of C versus C++ code
    By circuitbreaker in forum C++ Programming
    Replies: 20
    Last Post: 02-06-2008, 06:26 PM
  5. Time - not returning correct day
    By MethodMan in forum C Programming
    Replies: 8
    Last Post: 03-26-2003, 01:21 AM