0

I am trying to store an array of char pointer to another array of char pointer. I am getting segmentation fault for the same.

int main(int argc, const char* argv[])
{   
    int argcCpy = argc;
    char* argvCpy[10] = {};

    for(argcCpy = argc; argcCpy>0; argcCpy--)
    {   
        argvCpy[argcCpy] = (char *) malloc(strlen(argv[argcCpy]));
        memcpy(argvCpy[argcCpy], argv[argcCpy], strlen(argv[argcCpy]));
        printf("\nCount: %d, string: %s", argcCpy, argvCpy[argcCpy]);
    }
    return 0;
}

I spent more than enough time to make this work but I am not able to do it. Also, the same kind of question is already asked which is also left unanswered. If anybody can let me know the working code for the same, it would be really so helpful.

Hoping this to be answered.

Link of the similar question left out unawnsered -- C Beginner - Copying a char *array to another char *array

Thanks.

7
  • What is argvCpy[0] supposed to hold in your program? Garbage? Commented Apr 12, 2016 at 6:54
  • Why would I need a variable to hold a garbage value? Has been initialized !! Thanks for the comments :) @Lundin I need this var to hold the *argv array.. Commented Apr 12, 2016 at 7:03
  • My point is: your code doesn't copy anything into index 0 so it just sits there taking up space. This in turn suggests that either the array size in incorrect or the loop is not correctly written. Commented Apr 12, 2016 at 7:28
  • I need the values from arg 2 @Lundin.. Hence storing it in argvCpy from arg2 onwards. Commented Apr 12, 2016 at 7:35
  • But this code stores argv[x] to argv[1] into your array, in backwards order. So how many arguments do you need? 8? Then why declare an array of 10? Commented Apr 12, 2016 at 7:56

2 Answers 2

1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char* argv[])
{
    int argcCpy = argc;
    char* argvCpy[10] = {};

    if (argc > 9)
    {
        return 1;
    }

    for(int i = argc; i > 0; i--)
    {
        if (argv[i] == NULL)
        {
            argvCpy[i] = NULL;
        }
        else
        {
            argvCpy[i] = (char *) malloc(strlen(argv[i]) + 1);
            if (argvCpy[i] != NULL)
            {
                strcpy(argvCpy[i], argv[i]);
            }
        }
    }

    for (int i = 0; i <= argcCpy; i++)
    {
        if (argvCpy[i] != NULL)
        {
            printf("Count: %d, string: %s\n", i, argvCpy[i]);
        }
        else
        {
            printf("Count: %d, string is null\n", i);
        }
    }

    return 0;
}

Check argc is not too high. argv[argc] is NULL, take this into account. Use strcpy, and allocate enough room for the ending \0.

Edit: Second for loop to show content.

Sign up to request clarification or add additional context in comments.

10 Comments

Superb. Thanks a lot for your timely help. Its working. @jdarthenay
It is not obvious why they would want to store the program name argv[0] in the array though.
@ Lundin If you don't want it, set it to NULL, but do not leave it that way like in OP's code. Or index argvCpy so that argvCpy[i] stands for argv[i + 1]
@jdarthenay, Is there any ways that I can use the array out of the else without segmentaion fault..??
@Denise you should not get a segmentation fault. What are you doing exactly to get one?
|
1

You must allocate one byte more for the terminating NUL:

Change

 malloc(strlen(argv[argcCpy]);

to

 malloc(strlen(argv[argcCpy] + 1);

and you also must copy one byte more with memcpy

Change

 memcpy(argvCpy[argcCpy], argv[argcCpy], strlen(argv[argcCpy]));

to

 memcpy(argvCpy[argcCpy], argv[argcCpy], strlen(argv[argcCpy]) + 1);

BTW you can replace

 memcpy(argvCpy[argcCpy], argv[argcCpy], strlen(argv[argcCpy]) + 1);

by

 strcpy(argvCpy[argcCpy], argv[argcCpy]);

which is simpler and more clear.

And last but not least replace

for(argcCpy = argc; argcCpy>0; argcCpy--)

by

for(argcCpy = argc - 1; argcCpy>0; argcCpy--)

The last element of the argv array is argv[argc-1].

But be aware that you'll run into problems if you have more then 10 command line arguments.

4 Comments

Michael, it still outputs as segmentation fault.
Even simpler (since you are using malloc for memory allocation anyway): argvCpy[argcCpy] = strdup (argv[argcCpy]);
Two strings walk into a bar. One says "I'll have a beer, please794(*&%#$)${". The other says, "You'll have to excuse my friend, he's not null terminated." (That's one way of never forgetting again!)
Thank you lots @Mike & Micheal Walz