C语言 如何从字符数组复制到字符指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28087519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:42:21  来源:igfitidea点击:

How to copy from character array to character pointer?

c

提问by dcds

I am trying to copy from a character array to character pointer. My code:

我正在尝试从字符数组复制到字符指针。我的代码:

char str[] = "Hello World";
char *result = (char *)malloc(strlen(str)+1);
int index=0;
while(index <= strlen(str))
{
  *result = str[index];
  result++;
  index++;
}

This above code is not working and below code is working

上面的代码不起作用,下面的代码正在运行

char str[] = "Hello World";
char *result = (char *)malloc(strlen(str)+1);
int index=0;
while(index <= strlen(str))
{
  result[index] = str[index];
  index++;
}

Can anyone explain this behavior?

谁能解释这种行为?

回答by Rajalakshmi

we simply use the strcpy function to copy the array into the pointer.

我们简单地使用 strcpy 函数将数组复制到指针中。

     char str[] = "Hello World";
     char *result = (char *)malloc(strlen(str)+1);
     strcpy(result,str);

回答by Some programmer dude

In your first snippet you modify the pointer in the loop, so after the loop it will no longer point to the same location returned by the malloccall.

在您的第一个代码段中,您修改了循环中的指针,因此在循环之后它将不再指向malloc调用返回的相同位置。

Look at it this way, after the call to mallocthe pointer and the memory look like this:

这样看,在调用malloc指针和内存之后是这样的:

result
|
v
+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+

After the first iteration of the loop it will look like this:

在循环的第一次迭代之后,它将如下所示:

    result
    |
    v
+---+---+---+---+---+---+---+---+---+---+---+---+
| H |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+

And after the loop is done it will look like this

循环完成后,它看起来像这样

                                                 result
                                                 |
                                                 v
+---+---+---+---+---+---+---+---+---+---+---+----+
| H | e | l | l | o |   | W | o | r | l | d | 
char str[] = "Hello World";
char *result = (char *)malloc(strlen(str)+1);
strcpy(result,str);
| +---+---+---+---+---+---+---+---+---+---+---+----+

The copying is made, but the pointer no longer points to the original position.

复制完成,但指针不再指向原始位置。

[Note: Before the copying, the memory allocated by mallocwill not be "empty" or initialized in any way, I just show it like that here for simplicity's sake.]

[注意:在复制之前,分配的内存malloc不会是“空的”或以任何方式初始化,为了简单起见,我只是在这里展示它。]

回答by Esakki Thangam

Try this code.. Use strcpy() function.

试试这个代码.. 使用 strcpy() 函数。

 strcpy(result,str);

回答by Bhuvanesh

Already you allocate a memory for that pointer, so you can simply use the strcpy() function to copy the characters

您已经为该指针分配了内存,因此您可以简单地使用 strcpy() 函数来复制字符

char *result = ...
char *start = result;

str copied to result.

str 复制到结果。

回答by Theodoros Chatzigiannakis

In my opinion, there is no practical reason to modify your pointers (like you do in the first snippet) when you can just add an offset (like you do in the second snippet). It just makes the code a little bit harder to get right.

在我看来,当您可以添加偏移量(就像您在第二个代码段中所做的那样)时,没有实际理由修改您的指针(就像您在第一个代码段中所做的那样)。它只是使代码更难正确。

For the first snippet, try saving the original resultlike this:

对于第一个片段,尝试result像这样保存原始片段:

char str[] = "Hello World";
char *result = (char *)malloc(strlen(str)+1);
char *tmp = result;
int index=0;
while(index <= strlen(str))
{
  *result = str[index];
  result++;
  index++;
}
printf("%s\n", tmp);

And after the loop, try printing start, instead of result. It should point to the newly copied string, like you expect.

在循环之后,尝试打印start,而不是result。它应该指向新复制的字符串,就像您期望的那样。

回答by sudhir

Your first code snippet copied the string perfectly , but in this process you moved allocated character pointer. If it has to work save its initial value in some temporary pointer

您的第一个代码片段完美地复制了字符串,但在此过程中您移动了分配的字符指针。如果它必须工作,将其初始值保存在某个临时指针中

##代码##