Hi guys, I'm learning C and I still don't have some concepts very clear in my mind.
I have to create a program that get the values from ARGV[] and puts into a vector:

the program would work like that:

./program file1 file2 file3

My idea was to do something like:

char command[argc]

for(i<argv) { strcpy(command[i], argv[i+1]) }

But I get a warning.
I think I'm messing up the pointers, char, etc.

What I imagine I'm doing with that code line is creating someting like...

ARGV                     ------------->           command  
[program]                 strcpy...               [file1  ]
[file1  ]                                         [file2  ]
[file2  ]                                         [file3  ]
[file3  ]

...but probably its only in my head! Wtf am I doing ? How this should be written to work ???

char command[argc]

You need to make that an array of pointers: char* command[argc], then before copying the string from argv you will have to call malloc() to allocate memory for each string + 1 for the string's null terminator.

line 8: The for statement is incorrect format. --

char* command[argc];  // older compiles won't like this line
char**command = malloc(argc, sizeof(char*)); // alternate way to do it
for(i = 1; i < argc; ++i) 
   command[i-1] = malloc(strlen(argv[i])+1);
   strcpy(command[i-1], argv[i]); 

but can you explain me why ? What was I doing and what's the difference ? And also the difference between char** and char* ?

One last think,
After I get my array COMMAND[] with the strings from ARGV[] I wanted to create a function called "int control(int n, char COMMAND[])" but when I compile I get the following error:

22.c:40: warning: passing argument 2 of ‘paralello’ from incompatible pointer type

I think it has to do with the types "char** and char*"

char command[] is just a simple array of characters, or a single string such as "Hello". You can't store multiple strings all at the same time in it.

char* command[] is an array of pointers to strings, which is what you need for your program. Another way to express it is char** command.

All older compiler require a constant integer to expres the number of elments in array, such as char* command[20]. The newest C standard also allows it to be a variable, such as char* command[argv]. You might have to test it to find out which form your compiler supports. Which ever way your compiler accepts you have to be consistent throught the program, don't use char** command in one place then char* command[] in another because the compiler will complain about that too.

If you are still confused about pointers then maybe you should study this tutorial.

commented: perfect awnser! +0

Thanks man, perfect awnser!!!
am I able to call the funcion control(int n, char *COMMAND[])
with the array of pointers ?

If you declare the array like that: char* command[25]. Otherwise you can do his: conrol(int n, char** command). Note: variable names are NOT usually in UPPER CASE.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.