Thread: Array declaration doubt

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Question Array declaration doubt

    Hi everybody,
    I am new to this forum, so thank you for you replies
    My doubt is about arrays, it is wrong to declare array's size using variables? if not (as I expect), why exactly?
    a simple example is

    Code:
    int arr[a];
    where "a" is an integer variable.
    thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by finalsayan
    My doubt is about arrays, it is wrong to declare array's size using variables?
    Not necessarily.

    Quote Originally Posted by finalsayan
    if not (as I expect), why exactly?
    You would be making use of the variable length array language feature. However, this feature is relatively new, so it may not be available.
    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
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by laserlight View Post
    Not necessarily.
    can you make an example?
    Code:
    int arr[a];
    I guess that when the value of the varialble "a" is not know at compile time, then we could have problems, it is right? for example when the value of "a" is taken as a user input.

    Quote Originally Posted by laserlight View Post
    You would be making use of the variable length array language feature. However, this feature is relatively new, so it may not be available.
    I prefer to stay with classic C, without using these new features
    thank you

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    While I can use a variable to size and array I get warnings which I make a habit of eliminating !
    #defines ...is the only clean way I've seen in C.
    Code:
    /* I compile with full warnings :
    gcc -Wall -W -pedantic  arr1.c  -o  arr1
    */
    #include <stdio.h>
    
    #define SZ1 256
    
    static const int z = 256;
    
    int main(void)
    {
      int x  = 256;
      static const int y = 256;
    
      char buff1[x];      /* warning: ISO C90 forbids variable-size array ‘buff’ */
    
      char buff2[y];      /* warning: ISO C90 forbids variable-size array ‘buff’ */
    
      char buff3[z];      /* warning: ISO C90 forbids variable-size array ‘buff’ */
    
      char buff4[SZ1];    /* no warns */
    
      return 0;
    }
    I don't know what the reasoning is but I would imagine it's because they want to keep a static object far away from dynamic objects.
    Last edited by HowardL; 03-29-2010 at 09:13 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Howard, try compiling with
    Code:
    gcc -Wall -W -pedantic -std=c99 arr1.c -o arr1
    and see what happens. (I.e., using the 1999 version instead of the 1990 version of the language.)

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    -std=c99
    I see , no warnings now, thanks.
    Is that a good thing???
    I wonder why the greybeards thought it was not so good?
    Something in me trusts old school more than new school.
    Of course it sure could be handy when jumping to a new block.
    But I could see where it could be a problem if you don't keep your size variable straight while the array is still in scope and in use.
    That could lead to REAL trouble!
    Last edited by HowardL; 03-29-2010 at 09:45 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HowardL
    Is that a good thing???
    Yes, if your intention is to use variable length arrays.

    Quote Originally Posted by HowardL
    I wonder why the greybeards thought it was not so good?
    I cannot speak for any "greybeards", but I avoid VLAs in practice because I want my code to be more portable, and often to be compilable as C++.

    Quote Originally Posted by HowardL
    I could see where it could be a problem if you don't keep your size variable untouched while the array is still in scope and in use.
    That could lead to REAL trouble!
    Yeah, but this is the same when working with malloc() and company.
    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

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's not old school, vs new school, it's that it was a widely supported compiler extension that was added into C99. As long as the application will always be compiled in c99, this is not a problem. If compiled on an old compiler that does not have the extension, then there's a problem. If compiled in c90 mode then it issues a warning, because compiler extensions are not generally portable.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Yeah, but this is the same when working with malloc() and company.
    But then you KNOW your walking on eggshells and not lulled into a sense of complacency.

    I don't know, I've gotten by pretty well without using them so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM