Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. error C2143: syntax error

error C2143: syntax error

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    Fighting to solve old plan C errors, I met another issue: error C2143: syntax error I have the following code:

    typedef struct test test\_t;
    struct test
    {
    	const char\* name;
    	const char\* name\_option;
    	...
    	...
    };
    

    and
    test_t test_123 = {
    .name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
    .name_option = "Bibi_One",
    };

    How can I get rig of this error ?

    D S 2 Replies Last reply
    0
    • _ _Flaviu

      Fighting to solve old plan C errors, I met another issue: error C2143: syntax error I have the following code:

      typedef struct test test\_t;
      struct test
      {
      	const char\* name;
      	const char\* name\_option;
      	...
      	...
      };
      

      and
      test_t test_123 = {
      .name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
      .name_option = "Bibi_One",
      };

      How can I get rig of this error ?

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      This form of initialization is valid in GNU C, and is valid in other compilers only if they support C99 or later. I don't remember off-hand whether this is supported in C++. You can make this compatible with C89 by adding the missing initializers, e.g.:

      test_t test123 = { "Bibi", "Bibi_one", foo, bar };

      Where foo and bar are the values for missing members in the struct.

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      1 Reply Last reply
      0
      • _ _Flaviu

        Fighting to solve old plan C errors, I met another issue: error C2143: syntax error I have the following code:

        typedef struct test test\_t;
        struct test
        {
        	const char\* name;
        	const char\* name\_option;
        	...
        	...
        };
        

        and
        test_t test_123 = {
        .name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
        .name_option = "Bibi_One",
        };

        How can I get rig of this error ?

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        Get rid of the member names in the initalization, it's not valid in C++. You can only pass values, and these will be used to initialize the members of the struct in the order of their definition. You don't need to provide values for all members - if you don't, the rest will use default values:

        test_t test_123 = { "Bibi", "Bibi_One" };

        Special case: if you try to initialize a C array in this manner with less values than it's size, the remainder of the array will be initialized with the last value of your initializer list. This code will initialize your entire array with 1s:

        int my_array[9] = {1};

        You can read up on this topic in many articles on the web, just use the correct search term: initializer list.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        _ 1 Reply Last reply
        0
        • S Stefan_Lang

          Get rid of the member names in the initalization, it's not valid in C++. You can only pass values, and these will be used to initialize the members of the struct in the order of their definition. You don't need to provide values for all members - if you don't, the rest will use default values:

          test_t test_123 = { "Bibi", "Bibi_One" };

          Special case: if you try to initialize a C array in this manner with less values than it's size, the remainder of the array will be initialized with the last value of your initializer list. This code will initialize your entire array with 1s:

          int my_array[9] = {1};

          You can read up on this topic in many articles on the web, just use the correct search term: initializer list.

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Thank you all of you, it works.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups