简体   繁体   English

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

[英]How to copy from character array to character pointer?

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? 谁能解释这种行为?

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);

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 malloc call. 在您的第一个片段中,您修改循环中的指针,因此在循环之后它将不再指向malloc调用返回的相同位置。

Look at it this way, after the call to malloc the 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 | \0 |
+---+---+---+---+---+---+---+---+---+---+---+----+

The copying is made, but the pointer no longer points to the original position. 进行复制,但指针不再指向原始位置。

[Note: Before the copying, the memory allocated by malloc will not be "empty" or initialized in any way, I just show it like that here for simplicity's sake.] [注意:在复制之前, malloc分配的malloc不会是“空”或以任何方式初始化,为了简单起见,我只是在这里展示它。

Try this code.. Use strcpy() function. 试试这段代码..使用strcpy()函数。

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

Already you allocate a memory for that pointer, so you can simply use the strcpy() function to copy the characters 您已经为该指针分配了内存,因此您只需使用strcpy()函数来复制字符

 strcpy(result,str);

str copied to result. str复制到结果。

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 result like this: 对于第一个代码段,请尝试保存原始result如下所示:

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

And after the loop, try printing start , instead of result . 循环后,尝试打印start ,而不是result It should point to the newly copied string, like you expect. 它应该指向新复制的字符串,就像您期望的那样。

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 如果必须在一些临时指针中保存其初始值

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);

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

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