Confusing awkward array/struct syntax

This is a discussion on Confusing awkward array/struct syntax within the C Programming forums, part of the General Programming Boards category; I've been taking notice of some extra syntactical feature that arrays and structs (probably other elements of C/C++ as well) ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    71

    Confusing awkward array/struct syntax

    I've been taking notice of some extra syntactical feature that arrays and structs (probably other elements of C/C++ as well) have that aren't yet documented in your tutorials, nor any C/C++ books I've read.

    Example 1:
    Code:
    struct
    {
        int some_num;
        char[] some_string;
    } my_instance = {0}
    It seems normal up until the last line. What is this weird '= {0}' segment?

    Example 2:
    Code:
    int some_multiarray[4][5] =
    {{0,1,2,3,4},{0,1,2,3,4},{0,1,2,3,4},{0,1,2,3,4}}
    I could probably guess this is some sort of initialization, but if so, are these initial values, or constant values?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ex1: Same as Ex2. Just with 0.
    Note:
    Code:
    char[] some_string;
    From Ex1 is illegal. Too much Java perhaps? :-)

    Ex2: Yes, initial values (there is no const modifier in the declaration).
    Last edited by zacs7; 01-02-2009 at 02:30 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    Oh, it's just an example variable to fill the example struct with, no significance really... But that does work for me, though I've only tested a static char array.

    EDIT: Also, speaking of initial values in a struct, would this be valid? ((using the bool type fdrom C++ in this one))
    Code:
    struct
    {
        int number;
        bool status;
    } something = {5, false}
    Last edited by Chris87; 01-02-2009 at 02:44 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Quote Originally Posted by Chris87
    But that does work for me, though I've only tested a static char array.
    What is the exact program that you tested? Which compiler did you use?

    EDIT:
    Quote Originally Posted by Chris87
    Also, speaking of initial values in a struct, would this be valid? ((using the bool type fdrom C++ in this one))
    In C99, with <stdbool.h> included and with a terminating semi-colon, yes.
    Last edited by laserlight; 01-02-2009 at 03:03 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    MinGW GCC 3.4.5

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Quote Originally Posted by Chris87
    MinGW GCC 3.4.5
    That should fail to compile your first example code and should spew out a number of error messages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    Bugger... I'd use TDM's GCC 4.3.2 but I use toolkits like wxWidgets and what not that assume since I'm using MinGW32, I use 3.4.5... I think...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Um, the problem is not with the compiler, the problem is with the code, as zacs7 stated. The second example is fine, once you add the terminating semi-colon.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    Hmm... see, when I did it with an uninitialized pointer, it crashed, but when I did something like:
    Code:
    char name[16];
    It was ok.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by Chris87 View Post
    Hmm... see, when I did it with an uninitialized pointer, it crashed, but when I did something like:
    Code:
    char name[16];
    It was ok.
    That's because a char[16] is absolutely nothing like an uninitialized char *
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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