简体   繁体   中英

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.

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.

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.

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. 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! 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. Thanks!

Once you have declared

char* arg_list[6];

you cannot assign anything to 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;

Also, for argc items, the maximum subscript is [argc - 1], so you will want to adjust that loop.

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] :

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.


Warning: When printing variable data you always should use a format-string for 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. That effectively results in:

printf("%s");

Note that you do not supply the required string argument, but printf tries to read it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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