Is this standard C

This is a discussion on Is this standard C within the C Programming forums, part of the General Programming Boards category; Is the following code C99 code or does it happen to be an extension of my compiler? (gcc) Code: void ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    201

    Is this standard C

    Is the following code C99 code or does it happen to be an extension of my compiler? (gcc)

    Code:
    void func(void)
    {
        int a[10] = { [4] = 1, [7] = 1 };
    }
    
    this is the same as:
    
    void func(void)
    {
        int a[10] = { 0, 0, 0, 0, 1, 0, 0, 1 };
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Laserve
    Is the following code C99 code or does it happen to be an extension of my compiler? (gcc)

    Code:
    void func(void)
    {
        int a[10] = { [4] = 1, [7] = 1 };
    }
    
    this is the same as:
    
    void func(void)
    {
        int a[10] = { 0, 0, 0, 0, 1, 0, 0, 1 };
    }
    C99 Standard: yes
    C89 Standard: no

    Regards,

    Dave

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Yes,
    This is example is from C99 standard
    EXAMPLE 9 Arrays can be initialized to correspond to
    the elements of an enumeration by using designators:
    Code:
    enum { member_one, member_two };
    const char *nm[] = {
    [member_two] = "member two",
    [member_one] = "member one",
    };
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool Strange

    Code:
        int a[10] = { [4] = 1, [7] = 1 };
    well well well well. the [4]=1 here refers to a[4]=1
    and [7]=1 refers to a[7]=1.so they are one. but i couldnt understand how the remaining elements, a[0], a[1].....took zero as the default value.lemme check it out

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    C defaults to a value of 0 when you "attempt" to initialise it so to speak. Can't really explain it well but this code explains it a bit better:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            int a[10] = { [4] = 1, [7] = 1 };
            int b[10] = { };
            int c[10];
            int i;
    
            for (i = 0; i < 10; i++)
            {
                    printf("%d %d %d\n",a[i],b[i],c[i]);
            }
    
            return 0;
    }
    Notice how array "c" gets garbage values since we didn't initialise it at all, array "b" gets initialised to default 0 since we didn't specify what values we wanted. Array "a" also gets initialised to 0 unless specified, this causes the 5th and 8th cell of the array to hold a 1 due to our explicit initialisation.

  6. #6
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    I learn something new every day!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To explain it in simple terms, all uninitialized array memebers are set to zero. The only difference is where prior to C99, you could only do this:
    Code:
    int foo[10] = { 1, 2, 3 };
    Which fills the first three elements, and zero fills the rest. In C99, you can set specific values to whatever you like.
    Code:
    int foo[10] = { 1, 2, 3, [1] = 5, [4] = 7 };
    Giveing us an array containing:
    Code:
    { 1, 5, 3, 0, 7, 0, 0, 0, 0, 0 }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  2. Standard C++ API
    By sparks in forum C++ Programming
    Replies: 9
    Last Post: 08-26-2005, 05:35 PM
  3. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 01:35 PM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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