Custom struct not working in C
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Custom struct not working in C

  1. #1
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Custom struct not working in C

    Using VS2010, I defined a custom struct in the file vectorMath.h:
    Code:
    typedef struct Vector
    {
    	float v[3];
    };
    Then in main.c I have:
    Code:
    #include <vectorMath.h>
    And, later on,
    Code:
    Vector position;
    However, I get a compile error pointing out the line with Vector position; saying error C2061: syntax error : identifier 'position'. When I move the code for defining the struct inside main.c, above the declaration of position, I still get an error. I looked over tutorials for defining structs, but I don't see any error I'm making. Is there something fundamental I'm missing?

    There's two other custom structs, defined the same way, and they work fine. It seems ridiculous, but is there a maximum limit to the number of custom structs I can have?
    Last edited by fiodis; September 21st, 2012 at 07:14 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Custom struct not working in C

    However, I get a compile error pointing out the line with Vector position; saying error C2061: syntax error : identifier 'position'. When I move the code for defining the struct inside main.c, above the declaration of position, I still get an error. I looked over tutorials for defining structs, but I don't see any error I'm making. Is there something fundamental I'm missing?
    For 'C', that struct is not declared properly. Where is the typedef name for the struct?
    Code:
    typedef struct Vector
    {
    	float v[3];
    };
    What you see in red is not a typedef name -- it is the tag name. The typedef name would be something like this:
    Code:
    typedef struct tagVector
    {
    	float v[3];
    } Vector; // <-- here is the typedef name
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 21st, 2012 at 07:43 PM.

  3. #3
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Re: Custom struct not working in C

    That could be my problem. What's the purpose of the tag, then? MSDN says it could be used to refer to the structure type in code later, but why can't you just use the name of the type?

    Sorry about not posting a more complete program.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Custom struct not working in C

    Quote Originally Posted by fiodis View Post
    That could be my problem. What's the purpose of the tag, then? MSDN says it could be used to refer to the structure type in code later, but why can't you just use the name of the type?
    'C' and C++ are two different languages. Hopefully you're not reading the rules for C++.

    For 'C', here are valid structs:
    Code:
    /* definition 1 */
    struct Vector
    { };
    
    /* definition 2 */
    typedef struct tagVector
    { } Vector;
    With 'C', the first definition requires you to declare Vector like this:
    Code:
    struct Vector myVector;
    I had to repeat using the keyword struct to declare the myVector variable.

    With the second definition, I can now do this, since the typedef was used:
    Code:
    Vector myVector;
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 21st, 2012 at 08:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center