passing command line argument to array

Discussion in 'C Programming' started by virgincita schmidtmann, May 4, 2007.

  1. Good evening,

    I would like to pass the size of an array from the commandline.

    int main(int argc, int *argv[]) {
    ....
    max=*argv[1];

    int list[max];
    ....

    This does not work, because the size should be a constant.

    How can I work around this? I tried const or static, nothing works.

    Thank you,

    Ernst
    virgincita schmidtmann, May 4, 2007
    #1
    1. Advertising

  2. In article <f1g5fd$8od$01$-online.com>,
    virgincita schmidtmann <> wrote:

    >I would like to pass the size of an array from the commandline.


    >int main(int argc, int *argv[]) {
    >...
    > max=*argv[1];


    > int list[max];
    >...


    >This does not work, because the size should be a constant.


    >How can I work around this? I tried const or static, nothing works.


    What you propose is not legal in C89, but is (if I understand
    correctly) legal in C99 "variable length arrays".

    In C89, what you need to do is malloc() the space you need and
    store the returned pointer. int *list = malloc(max * sizeof int)
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, May 4, 2007
    #2
    1. Advertising

  3. On Fri, 4 May 2007 22:33:25 +0200, "virgincita schmidtmann"
    <> wrote:

    >Good evening,
    >
    >I would like to pass the size of an array from the commandline.
    >
    >int main(int argc, int *argv[]) {
    >...
    > max=*argv[1];
    >
    > int list[max];


    Even under a C99 compiler, this will not do what you want. *argv[1]
    is, by definition, a character. If it happened to be the character
    '2' and you run this on an ASCII system, it will define an array of 50
    int. And if the command line argument was the value "75", you would
    not allocate enough space. On an EBCDIC system, '2' would produce an
    array 242 int.

    >...
    >
    >This does not work, because the size should be a constant.


    This is true in C89 but C99 does allow variable length arrays.

    >
    >How can I work around this? I tried const or static, nothing works.


    Since you don't have a C99 compiler, the solution is in two parts:

    First convert the data argv1 points at to an integer. strtol
    would be the method of choice because it provides a convenient method
    for error checking.

    Second, allocate memory for the desired array and store the
    address of this memory in a pointer. malloc is a good choice here.
    You can then refer to elements of the array using normal array
    notation such as ptr.


    Remove del for email
    Barry Schwarz, May 5, 2007
    #3
  4. virgincita schmidtmann

    Joe Wright Guest

    virgincita schmidtmann wrote:
    > Good evening,
    >
    > I would like to pass the size of an array from the commandline.
    >
    > int main(int argc, int *argv[]) {
    > ...
    > max=*argv[1];
    >
    > int list[max];
    > ...
    >
    > This does not work, because the size should be a constant.
    >
    > How can I work around this? I tried const or static, nothing works.
    >
    > Thank you,
    >
    > Ernst
    >
    >

    Create a dynamic array with malloc?

    int *list;
    int max;
    max = atoi(argv[1]);
    list = malloc(max * sizeof *list);

    The above is not a program, it is only a clue. The task is yours.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, May 5, 2007
    #4
  5. Thank you all!

    1. Mhmm. If I allocate memory to a pointer, can I use this pointer
    without defining an array?

    I think, array is a pointer to the first element of the array. Memory
    size from malloc and list[] is the same. Does the compiler have to
    know the size of an element in the array?

    2. In the past I took integer arguments from the commandline by
    defining argv[] as int. Well, there is a problem with argv[0], because
    it is definitely the name of the program. But if I don't use it?

    3. I am not really sure, if I need a variable length array.

    However, I will try it.

    Best regards,

    Ernst

    Barry Schwarz wrote:
    > On Fri, 4 May 2007 22:33:25 +0200, "virgincita schmidtmann"
    > <> wrote:
    >
    >
    >>Good evening,
    >>
    >>I would like to pass the size of an array from the commandline.
    >>
    >>int main(int argc, int *argv[]) {
    >>...
    >> max=*argv[1];
    >>
    >> int list[max];

    >
    >
    > Even under a C99 compiler, this will not do what you want. *argv[1]
    > is, by definition, a character. If it happened to be the character
    > '2' and you run this on an ASCII system, it will define an array of 50
    > int. And if the command line argument was the value "75", you would
    > not allocate enough space. On an EBCDIC system, '2' would produce an
    > array 242 int.
    >
    >
    >>...
    >>
    >>This does not work, because the size should be a constant.

    >
    >
    > This is true in C89 but C99 does allow variable length arrays.
    >
    >
    >>How can I work around this? I tried const or static, nothing works.

    >
    >
    > Since you don't have a C99 compiler, the solution is in two parts:
    >
    > First convert the data argv1 points at to an integer. strtol
    > would be the method of choice because it provides a convenient method
    > for error checking.
    >
    > Second, allocate memory for the desired array and store the
    > address of this memory in a pointer. malloc is a good choice here.
    > You can then refer to elements of the array using normal array
    > notation such as ptr.
    >
    >
    > Remove del for email
    Ernst Schmidtmann, May 5, 2007
    #5
  6. Please don't top-post. Put what you're replying below what you cite
    from the previous post.

    Ernst Schmidtmann <> wrote:
    > 1. Mhmm. If I allocate memory to a pointer, can I use this pointer
    > without defining an array?


    You don't allocate "momory to a pointer", you allocate memory and
    assign the address you receive from malloc() to a pointer. And
    yes, of course, you can then use the pointer to access the
    memory you allocated in an array-like fashion using the pointer.

    > I think, array is a pointer to the first element of the array.


    Definitely not. An array is an array, not a pointer. Only when
    the compiler finds an array in a place where a value is required
    the array is replaced by a pointer to the first element of the
    array. This happens e.g. when an array is used as a function
    argument since in C all function arguments are passed by value
    but an array isn't a value. And thus in this situation it gets
    converted to a value, which is the address of its first element.

    > Memory
    > size from malloc and list[] is the same. Does the compiler have to
    > know the size of an element in the array?


    If you create an array the compiler alreays knows the size
    of the elements from the type of the array. But if you call
    malloc() you need to tell malloc() exactly how much memory
    you need, e.g. for enough memory to store 75 ints you do

    int *ip;
    ip = malloc( 75 * sizeof *ip );

    where 'sizeof *ip' is the number of bytes required for the
    type 'ip' points to, in this case the number of bytes needed
    to store an int.

    > 2. In the past I took integer arguments from the commandline by
    > defining argv[] as int. Well, there is a problem with argv[0], because
    > it is definitely the name of the program. But if I don't use it?


    Then you did something wrong. The elements of argv are all
    pointers to strings, pointing to strings with the command
    line arguments (except argv[0] and the very last element,
    which is always NULL). So if your first command line argu-
    ment was 2 then argv[1] points to the string "2". And if
    you now do

    int max = *argv[1];

    then you assign to 'max' the numerical value of the character
    '2' and _not_ the integer value 2. And if the command line
    argument had been 75 the argv[1] would point to the string
    "75" and with the above assignment you would assign the
    numerical value of the character '7' to 'max' and neither
    the integer value 7 nor 75. You first have to convert the
    string you got to an integer, using e.g. strtol().

    > 3. I am not really sure, if I need a variable length array.


    If you need memory with a size that only can be determined at
    run-time and not already when the program gets compiled then
    you either need a variable length array (which requires a com-
    piler that implements at least this part of C99) or you must
    obtain the memory via malloc().

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 5, 2007
    #6
  7. virgincita schmidtmann

    Old Wolf Guest

    On May 5, 8:33 am, "virgincita schmidtmann" <>
    wrote:
    >
    > int main(int argc, int *argv[]) {


    This is an error. Not sure why nobody else noticed it;
    perhaps they subconsciously filitered it out !

    The second argument to main must have 'char' where
    you current have 'int'. Who knows what your compiler
    is doing with that code. Probably not what you expect!
    Old Wolf, May 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hayri ERDENER

    command line argument passing

    Hayri ERDENER, Jul 22, 2005, in forum: Python
    Replies:
    3
    Views:
    396
    Dennis Benzinger
    Jul 22, 2005
  2. Alan Biddle
    Replies:
    14
    Views:
    2,567
    John Bokma
    Feb 5, 2010
  3. Trans
    Replies:
    3
    Views:
    196
    Robert Klemme
    Jan 26, 2005
  4. kenny roytman
    Replies:
    0
    Views:
    137
    kenny roytman
    Oct 30, 2007
  5. ±è ÁØ¿µ
    Replies:
    7
    Views:
    251
    Einar Magnús Boson
    Nov 27, 2008
Loading...

Share This Page