如何字符指针数组复制到另一个用C [英] How to copy an array of char pointer to another in C

查看:214
本文介绍了如何字符指针数组复制到另一个用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想字符指针数组存储字符指针的另一个数组。我正在分割过错一样。

  INT主(INT ARGC,为const char * argv的[])
{
    INT argcCpy = ARGC;
    字符* argvCpy [10] = {};    为(argcCpy =的argc; argcCpy大于0; argcCpy--)
    {
        argvCpy [argcCpy] =(字符*)malloc的(strlen的(的argv [argcCpy]));
        的memcpy(argvCpy [argcCpy],ARGV [argcCpy]的strlen(的argv [argcCpy]));
        的printf(\\ NCOUNT:%D,字符串:%S,argcCpy,argvCpy [argcCpy]);
    }
    返回0;
}

我花了足够的时间更多,使这项工作,但我不能做到这一点。此外,同一种问题已经问这也没有答案。如果任何人都可以让我知道了工作code为相同,这将是非常有用的如此。

希望这得到回答。

冷落unawnsered了类似的问题的链接 - <一个href=\"http://stackoverflow.com/questions/21290255/c-beginner-copying-a-char-array-to-another-char-array\">C初级 - 复制一个char *数组到另一个字符*阵列

感谢。


解决方案

 的#include&LT;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,为const char * argv的[])
{
    INT argcCpy = ARGC;
    字符* argvCpy [10] = {};    如果(ARGC&GT; 9)
    {
        返回1;
    }    的for(int i = ARGC; I&GT; 0;我 - )
    {
        如果(的argv [I] == NULL)
        {
            argvCpy [I] = NULL;
        }
        其他
        {
            argvCpy [I] =(字符*)malloc的(strlen的(的argv [I])+ 1);
            如果(argvCpy [I]!= NULL)
            {
                的strcpy(argvCpy [I]的argv [I]);
            }
        }
    }    的for(int i = 0; I&LT; = argcCpy;我++)
    {
        如果(argvCpy [I]!= NULL)
        {
            的printf(数数:%d,字符串:%S \\ n,我,argvCpy [I]);
        }
        其他
        {
            的printf(数数:%d,字符串为空\\ n,I);
        }
    }    返回0;
}

检查ARGC不太高。
ARGV [ARGC] 为NULL,考虑到这一点。
使用的strcpy ,并为结束拨出足够的空间 \\ 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.

解决方案

#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.

这篇关于如何字符指针数组复制到另一个用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆