For a program, I would like to make an array copy of the arguments sent in by command line using malloc().
对于程序,我希望使用malloc()创建命令行发送的参数的数组副本。
So for example if I do ./a.out one two three I want an array with {a.out, one, two, three} in it.
举个例子。我想要一个带{a的数组。输出,1 2 3}。
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!
感谢任何帮助!
3 个解决方案
#1
3
You're not allocating or copying the terminating NULL characters:
您没有分配或复制终止的空字符:
This line needs to be changed to this for the NULL.
对于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.
此外,如果保存了strlen()调用的结果,则可以更好地优化代码,因为您在很多地方调用它。
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];
}
}
#2
2
first you need to allocate a vector of char*, not just a char*
首先,您需要分配一个char*的向量,而不仅仅是一个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
现在有一个数组[0.. .]命令行参数个数)的char *指针
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) {...} */
#3
0
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
, ..., ...)
定义char*类型的对象。也就是说:一个值可以指向char的对象(以及下一个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
.
对于这种新类型,数组的值指向char*(另一个指针)。您可以分配内存并将已分配内存的地址保存在char*中,而不使用char。
#1
3
You're not allocating or copying the terminating NULL characters:
您没有分配或复制终止的空字符:
This line needs to be changed to this for the NULL.
对于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.
此外,如果保存了strlen()调用的结果,则可以更好地优化代码,因为您在很多地方调用它。
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];
}
}
#2
2
first you need to allocate a vector of char*, not just a char*
首先,您需要分配一个char*的向量,而不仅仅是一个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
现在有一个数组[0.. .]命令行参数个数)的char *指针
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) {...} */
#3
0
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
, ..., ...)
定义char*类型的对象。也就是说:一个值可以指向char的对象(以及下一个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
.
对于这种新类型,数组的值指向char*(另一个指针)。您可以分配内存并将已分配内存的地址保存在char*中,而不使用char。