Variable Array Size From Command Line Argument?

This is a discussion on Variable Array Size From Command Line Argument? within the C Programming forums, part of the General Programming Boards category; Hey guys, I am wondering if it is possible to receive a value from a command line that creates an ...

  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
    22,314
    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.
    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

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 08: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, 10:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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