Thread: why a[i] is not compiling . array compilation error

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    why a[i] is not compiling . array compilation error

    Hi,

    i tried to compile the below program

    1st program

    Code:
    int main()
    {
            int i=10;
            int a[2]={i,i+1};
            printf ("hai %d\n",a[0]);
            return 0;
    }

    2nd program
    Code:
    int main()
    {
            int i=10;
            int j=2;
            int a[j]={i,i+1};
            printf ("hai %d\n",a[0]);
            return 0;
    }
    Why i am able to compile first program and getting compile error for second program?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard, your array a must have a fixed size known at compile time, so j cannot be a variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    Unless you are able to make use of the variable length array feature introduced in the 1999 edition of the C standard, your array a must have a fixed size known at compile time, so j cannot be a variable.
    Translation: You are still using Turbo C!


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    is it variable length array features is compiler dependent?

    I am using the gcc compiler version (gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)). my compiler will support variable length array feature?

    can you let me know small c program with variable length array feature?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can compile with compile flag: -std=c99

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    is it variable length array features is compiler dependent?
    In theory, no. In practice, yes, because not all C compilers in present use conform sufficiently to C99.

    Quote Originally Posted by nkrao123@gmail.
    I am using the gcc compiler version (gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)). my compiler will support variable length array feature?
    It should. You may need to pass -std=c99 as a compiler option.

    Quote Originally Posted by nkrao123@gmail.
    can you let me know small c program with variable length array feature?
    Your second program is such an example program, except that you cannot initialise the array like that. Oh and remember to #include <stdio.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Newer versions of gcc let this slide with a warning. I didn't start C programming until after gcc 4.+ was released, so I'm guessing that 3.4.5 does not do that. Try compiling:

    gcc -Wall -std=c99

    You can also use "-std=gnu99" which might be slightly more compatible with the default on linux (std=gnu89).

    If that doesn't work, post the actual error you are getting.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI light and Mk27,

    i am getting error if i compile with "gcc name.c -std=c99" and "gcc name.c -std=gnu99"

    error

    gcc 22.c -std=gnu99
    22.c: In function `main':
    22.c:6: error: variable-sized object may not be initialized
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')



    gcc 22.c -std=c99
    22.c: In function `main':
    22.c:6: error: variable-sized object may not be initialized
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')
    22.c:6: warning: excess elements in array initializer
    22.c:6: warning: (near initialization for `a')




    i fell my compiler may not support this feature. am i right?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I told you precisely what you did wrong: "except that you cannot initialise the array like that", as did your compiler: "variable-sized object may not be initialized".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nkrao123@gmail. View Post
    22.c:6: error: variable-sized object may not be initialized
    Try actually reading the error message. Also try reading what people actually write to you:
    Quote Originally Posted by laserlight View Post
    I told you precisely what you did wrong
    ^ See that?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    kotin
    Join Date
    Oct 2009
    Posts
    132
    It should. You may need to pass -std=c99 as a compiler option.
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nkrao123@gmail. View Post
    Now i understand that my compilr does not supporting this feature
    No, no you don't. You don't understand at all, because you aren't reading what people are telling you over and over.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature
    You misunderstand because you jumped to a conclusion without reading the rest of what I wrote. Try this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        size_t n = 10;
        int numbers[n];
    
        for (size_t i = 0; i < n; ++i)
        {
            numbers[i] = i * 2;
        }
    
        for (size_t i = 0; i < n; ++i)
        {
            printf("%d\n", numbers[i]);
        }
    
        return 0;
    }
    There is no need for numbers to be a variable length array in the above program, but perhaps the value of n could have been obtained from input instead.

    Now, this program should compile, once you add braces for the main function:
    Code:
    int main(void)
    return 0;
    Attempt to compile it. Compilation fails. You can thus jump to the conclusion that your C compiler does not support compiling even such a trivial C program
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    Light used that word as it should. I thought my compiler may have that features. That why i tried. Now i understand that my compilr does not supporting this feature
    Sorry, partially my mistake. As per laserlight's post above, what c99 will allow is this:

    Code:
    int array[x];
    Where x may be determined at runtime. Which means you cannot initialize it to any value, ie.

    Code:
    int array[x] = {1, 2, 3};
    is no good. You can use a "variable-sized object" but you can't initialize it in the declaration.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And because you still won't understand what they're saying:
    Quote Originally Posted by MK27 View Post
    Which means you cannot initialize it to any value, ie.

    Code:
    int array[x] = {1, 2, 3};
    is no good. You can use a "variable-sized object" but you can't initialize it in the declaration.
    You can do what is in green.
    You can't do the part in red.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation error on array assignment.
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 02:50 AM
  2. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  3. Compilation Error. Need help
    By CS_Student8337 in forum C Programming
    Replies: 4
    Last Post: 02-11-2009, 10:57 AM
  4. help about compilation error
    By netwizio in forum Linux Programming
    Replies: 2
    Last Post: 01-16-2004, 06:22 PM
  5. compilation error
    By blue_gene in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2003, 12:49 AM