简体   繁体   English

C:Malloc字符串数组混乱

[英]C: Malloc String Array Confusion

I'm attempting to allocate sufficient space to hold all command line arguments given to an array arg_list in reverse order. 我试图分配足够的空间来容纳以相反顺序分配给数组arg_list所有命令行参数。

char* arg_list[6];
arg_list = malloc((sizeof(char*)) * argc);
for (i=argc; i < 0; i--)
{
    arg_list[i]=argv[i];  
}

My theory was on malloc to get the sizeof a char* and then multiply that by how many arguments that were given, argc giving the total amount of space needed. 我的理论是在malloc上获取char*sizeof ,然后将其乘以给出的参数个数, argc给出所需的总空间量。

Then using the for loop, start i at the number of elements, argc , say 5 for example, and then put the 5th element of argv into the 5th spot in arg_list and do this until it gets to 0. 然后使用for循环,将i从元素数量argc ,例如说5,然后将argv的第5个元素放入arg_list的第5个位置,并执行直到达到0。

I'm getting warnings of incompatible implicit declaration and an error of assignment to expression with array type and am not sure where I'm going wrong. 我收到警告,提示incompatible implicit declarationassignment to expression with array type的错误,并且不确定我要去哪里。 Also, I've been using C for about a day and a half now, so dumb everything down as much as possible if you could! 另外,我已经使用C大约一天半了,因此,如果可以的话,请尽可能使所有内容变得愚蠢! I'd really appreciate it! 我真的很感激!

EDIT: Trouble Printing out the Reversed array Code: 编辑:麻烦打印出反向数组代码:

    char** arg_list = malloc((sizeof(char*)) * argc);

/** arg_list points to the command-line arguments in the */
/**     reversed order */
for (i=argc-1; i >= 0; i -=1)
{
    arg_list[i]=argv[i];  
}

/** print the content of arg_list */
/** fill here */
for (i=0; i<argc; i++)
{
    printf(arg_list[i]);
    printf("\n");
}

I am confused on how I would go about printing the reversed order. 我对如何打印反向订单感到困惑。 Whenever I print it, it prints in the normal order and not reverse. 每当我打印时,它都以正常顺序打印,而不是反向打印。 I am confused on how it gets put in arg_list in the normal order instead of the reverse. 我对如何以正常顺序而不是相反的顺序将其放入arg_list感到困惑。 Thanks! 谢谢!

Once you have declared 一旦声明

char* arg_list[6];

you cannot assign anything to arg_list . 您不能将任何东西分配给arg_list You can only assign to its elements. 您只能分配给它的元素。 Hence, 因此,

arg_list = malloc(...);

is wrong. 是错的。 You can use: 您可以使用:

arg_list[0] = malloc(...);

but that's not what you are after. 但这不是你所追求的。

You need to use: 您需要使用:

char** arg_list = malloc((sizeof(char*)) * argc);

You want char **arg_list; 您需要char **arg_list;

Also, for argc items, the maximum subscript is [argc - 1], so you will want to adjust that loop. 另外,对于argc项目,最大下标为[argc - 1],因此您将需要调整该循环。

I'm answering on the edited version which already includes the corrections from the other answers and comments. 我正在回答的编辑版本已经包含其他答案和评论的更正。

Lean back and have a look at 靠边看看

for ...
    arg_list[i] = argv[i];

So, you assign the pointer at position [i] to the pointer at position [i] : 所以,你在位置的指针分配[i]指针在位置[i]

arg_list[argc-1] = argv[argc-1];
...
arg_list[1] = argv[1];
arg_list[0] = argv[0];

Now think what reverse means ... 现在想想反向意味着什么...

Try to follow me. 试着跟着我。 Drop a comment if you are done (either successful or not) and I'll provide the full solution. 如果完成(成功与否),请发表评论,我将提供完整的解决方案。 But please try yourself first, it really is very simple and it will help you much more than a fully presented solution. 但是请先尝试一下,它确实非常简单,并且比完整介绍的解决方案有更多帮助。


Resolution: You have to index both arrays from different directions: 解决方法:您必须从不同方向为两个数组建立索引:

for (int i = 0 ; i < argc ; i++ )
    arg_list[i] = argv[ argc - 1 - i];

I changed to loop to the more obvious incrementing loop,as the direction does not matter. 由于方向无关紧要,我将循环更改为更明显的递增循环。

Note that the index of the uppermost entry is argc - 1 , not argc , as in C - as in most programming languages - indexes run from 0. 请注意,最上面的条目的索引是argc - 1 ,而不是argc ,就像C-像大多数编程语言一样-索引从0开始运行。


Warning: When printing variable data you always should use a format-string for printf & family. 警告:打印变量数据时, 始终应为printf &family使用格式字符串。 This becomes mandatory when prionting externally supplied data like the command-line arguments. 当重整外部提供的数据(例如命令行参数)时,这是强制性的。 Failing to do widely opens a security hole. 不能广泛执行将打开一个安全漏洞。 Just think about supplying %s as argument. 只需考虑提供%s作为参数即可。 That effectively results in: 有效地导致:

printf("%s");

Note that you do not supply the required string argument, but printf tries to read it. 请注意,您没有提供必需的字符串参数,但printf尝试读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM