Hello,
I have to write a program using exec() where user can specify the pathname and arguments. I am trying to read in the arguments in the following way:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
  ...
  int i;
  char *cmd[100];
  char argument[100];
  
  i = 0;
  for(i=0;i<3;i++)
  {
    printf("Enter %d. argument: ",i);
    scanf("%s",argument);
    cmd[i]=argument;
  }
  cmd[3] = (char *)0 ;

...

return 0;
}

But all I get is the last inputted argument in all 3 strings. I don't understand why. Could anyone help me with this?

Anna

char *cmd[100]; /* this is an array of pointers */
char argument[100]; /* this is just ONE array of 100 elements long */

cmd[i]=argument; /* Each time a new pointer element is pointing to the same array string */

You are changing the string every time through the loop. All those pointers points to the same string.

char argument[100] should be char argument[100][x]; /* where x is the length of the strings */

I thought so, but if I do it as:

cmd[0]="some_string";
cmd[1]="another_string";
etc.

it works fine.

I thought so, but if I do it as:

cmd[0]="some_string";
cmd[1]="another_string";
etc.

it works fine.

Yes, why do you think it wouldn't work?
cmd[0] is pointing to a literal string in memory containing the "some_string"
cmd[1] is pointing to anoter literal string in memory containig the "another_string",
Two different places in memory. Now:
cmd[0] = argument; is pointing to argument[100] a variable string in memory.
cmd[1] = argument; is pointing to the same place in memory than the previous pointer is.
You change argument[100] and each pointer will still be pointing to whatever argument hold.

[Edit:]
Wait a minute. Are you trying to copy whatever is in argument[100] into cmd?

I see. I used an array of strings as you adviced me and it works like this. Thank you.

Yes, I was trying to copy it. Is it fine or should I rather malloc cmd and point it to the input directly without the variables?

I mean I understand now that I'm not copying the string, but only its address in the memory. I just wonder if its better like this or without these variables.

>Yes, I was trying to copy it
Did you figure out why it would not work to try to copy into a pointer?
A pointer like cmd[0] can hold only the address location of some place in memory.
cmd[0] = argument; will hold only the address location of the first element of argument.

Things like cmd[0] = "Some_string" is deceiving in the way that it makes you think that
cmd[0] holds the value string. However all that is doing is pointing to the `S' of Some_string somewhere in `read only' memory. That's why you would never be able to change "Some_string".

>Is it fine or should I rather malloc cmd and point it to the input directly without the variables?
If you know how many strings you need I don't see why you need to go directly using malloc to do it for you.

Yes, I understand it now. Thank you.

I don't know how many strings I need.

>I don't know how many strings I need.
Well then you might need to make use of malloc and create the room for it as you go into the heap memory.

This article has been dead for over six months. Start a new discussion instead.