开发者

Copying Command Line Arguments Into an Array

开发者 https://www.devze.com 2023-04-12 16:40 出处:网络
For a program, I would like to make an array copy of the arguments sent in by command line using malloc().

For a program, I would like to make an array copy of the arguments sent in by command line using malloc().

So for example if I do ./a.out one two three I want an array with {a.out, one, two, three} in it.

However, I have some issues getting my program to work. Here's what I have:

static char** duplicateArgv(int argc, char **argv)                                                                                                                                                         
{                                                                                                                                                                                                                                                                                                                                                                                       
    char *array;                                                                                                                                                                                                                                                                                                                                                               
    int j = 0;                                         开发者_运维问答                                                                                                                                               

    // First allocate overall array with each element of char*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    array = malloc(sizeof(char*) * argc);                                                                                                                                                       
    int i;                  

    // For each element allocate the amount of space for the number of chars in each      argument                                                                                                                                                              
    for(i = 1; i < (argc + 1); i++){                                                                                                                                                                      
        array[i] = malloc(strlen(*(argv + i)) * sizeof(char));                                                                                                                                        
        int j;       

        // Cycle through all the chars and copy them in one by one                                                                                                                                                                                 
        for(j = 0; j < strlen(*(argv + i)); j++){                                                                                                                                                     
            array[i][j] = *(argv + i)[j];                                                                                                                                                                                                                                                                                                                                                       
        }                                                                                                                                                                                             

    }                                                                                                                                                                                                 

    return array;

}

As you might imagine, this doesn't work. I apologize ahead of time if this somehow totally doesn't make sense, as I just started learning about pointers. Also, I'm not quite sure how to write code to free up every element in the *array after I do what I need to the copy.

Could anyone give me some tips on what I should look into to make it do what I want?

Thanks for any help!


You're not allocating or copying the terminating NULL characters:

This line needs to be changed to this for the NULL.

array[i] = malloc((strlen(*(argv + i)) + 1) * sizeof(char));   

And the loop should be changed to this:

for(j = 0; j <= strlen(*(argv + i)); j++){ 

Also, the code can be better optimized if you saved the result of the strlen() call since you call it in so many places.

Try the loop as this:

// For each element allocate the amount of space for the number of chars in each argument
for(i = 0; i < argc; i++){

    int length = strlen(argv[i]);

    array[i] = malloc((length + 1) * sizeof(char));
    int j;

    // Cycle through all the chars and copy them in one by one
    for(j = 0; j <= length; j++){
        array[i][j] = argv[i][j];
    }

}


first you need to allocate a vector of char*, not just a char*

char **array;

array = malloc(sizeof(char*)*(argc+1)); // plus one extra which will mark the end of the array

now you have an array[0..argc] of char* pointers

then for each argument you need to allocate space for the string

int index;
for (index = 0; index < argc; ++index)
{
   arrray[index] = malloc( strlen(*argv)+1 ); // add one for the \0
   strcpy(array[index], *argv);
   ++argv;
}
array[index] = NULL; /* end of array so later you can do while (array[i++]!=NULL) {...} */


With

char *array;

you define an object of type char*. That is: an object which value can point to a char (and the next char, ..., ...)

You need

char **array;

With this new type, the value of array points to a char*, ie another pointer. You can allocate memory and save the address of that allocated memory in a char*, you't do that with a char.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号