struct initialization works one way, but not the other

This is a discussion on struct initialization works one way, but not the other within the C Programming forums, part of the General Programming Boards category; Why is this? Code: #include <stdio.h> struct idata_t { int max, cmax, pline; char *name, *data[100]; }; int main(void) { ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048

    Question struct initialization works one way, but not the other

    Why is this?
    Code:
    #include <stdio.h>
    
    struct idata_t {
        int max, cmax, pline;
        char *name, *data[100];
    };
    
    int main(void) {
        struct idata_t one = {.max = 2, .cmax = 0, .pline = 1, .name = "hippo", .data = 0};
    
        struct idata_t two = {.max = 2, .data = 0, .pline = 1, .name = "hippo", .cmax = 0};
    
        return 0;
    }
    The line in red causes a syntax error, while the green one doesn't!

    I'm using Dev-C++ 4 if that helps (4, not 4.9.9.2).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Should it be like this?
    Code:
    struct idata_t two = { .max = 2, .data = {0}, .pline = 1, .name = "hippo", .cmax = 0 };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Oh, yes, sorry. But I got lots of errors on the red line:
    Code:
    unknown field `pline' specified in initializer
    warning: initialization makes pointer from integer without a cast
    unknown field `name' specified in initializer
    unknown field `cmax' specified in initializer
    The warning is what you pointed out. But what about the other three?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Oh, you're right. I changed the
    Code:
    .data = 0
    to
    Code:
    .data = {0}
    and it compiled perfectly.

    Thanks.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Replies: 8
    Last Post: 12-21-2008, 09:42 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21