簡體   English   中英

將命令行參數復制到數組中

[英]Copying Command Line Arguments Into an Array

對於程序,我想使用malloc()對命令行發送的參數進行數組復制。

因此,例如,如果我執行./a.out一二三,我想要一個數組,其中包含{a.out,一,二,三}。

但是,我在使程序正常運行時遇到了一些問題。 這是我所擁有的:

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;

}

如您所料,這是行不通的。 如果這完全不合時宜,我會提前道歉,因為我剛剛開始學習指針。 另外,我不太確定在對副本執行所需的操作后如何編寫代碼以釋放* array中的每個元素。

任何人都可以給我一些提示,讓我了解如何使它做我想要的事情?

謝謝你的幫助!

您沒有分配或復制終止NULL字符:

需要將此行更改為NULL。

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

循環應更改為:

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

另外,如果保存了strlen()調用的結果,則代碼可以得到更好的優化,因為在很多地方都可以對其進行調用。

嘗試如下循環:

// 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];
    }

}

首先,您需要分配一個char *向量,而不僅僅是char *

char **array;

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

現在您有了char*指針的array[0..argc]

然后對於每個參數,您需要為字符串分配空間

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) {...} */

char *array;

您定義一個char*類型的對象。 也就是說:一個對象,其值可以指向一個char (以及下一個char ,...,...)

你需要

char **array;

對於這種新類型, array的值指向char* ,即另一個指針。 您可以分配內存並將已分配內存的地址保存在char* ,而無需使用char

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM