Thread: Variable Array Size From Command Line Argument?

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    Variable Array Size From Command Line Argument?

    Hey guys,

    I am wondering if it is possible to receive a value from a command line that creates an array of that size?

    Is the more 'sophisticated' method of doing this to use malloc()?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cell
    I am wondering if it is possible to receive a value from a command line that creates an array of that size?
    Yes, it is possible.

    Quote Originally Posted by Cell
    Is the more 'sophisticated' method of doing this to use malloc()?
    I am not sure how 'sophisticated' it is, but it is the usual way. In C99 you also have the option of variable length arrays.
    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
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Thank you.

    So if I am using a VLA, is it as simple as:

    Code:
    	
    int TEST;
    	
    printf("Message size?\n");
        	
    scanf("%d", &TEST);
      
    int string[TEST];
    Because I have been having some issues using this.

    Thanks.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Cell View Post
    So if I am using a VLA, is it as simple as:

    Because I have been having some issues using this.
    yes.

    Are you compiling in the C99 mode?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Is C99 mode standard in gcc?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it compiles without warnings with you scattering variables throughout your code like that, then yes, it's in C99 (or some bastardized) mode.

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

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Cell View Post
    Is C99 mode standard in gcc?
    The option to force GCC to use the C99 standard is "-std=c99" I believe.
    You should confirm with your man pages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  3. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM