0

I am trying to copy the String an char pointer to another ?

I tried the code given below .compiled successfully but output is blank(no output) .

a) Where I was wrong?where my programming logic getting fail?

b) How can I improve this code to get desired output?

    void main()
    {
     char *p="krishna";
     char *q;

      while(*p!='\0')
      {
       *q++=*p++;
      }
      printf("%s",q);
      getch();
    }
4

10 回答 10

3
void main()
{
  char *p="krishna";
  char *q;

  /* When copying, you modify your pointers, so you need another one
  ** to store where your string is starting. */
  char *r;

  /* Until now, q points to nowhere.
  ** You need to allocate the place it should point: */
  r = q = malloc(strlen(p) + 1);
  /* Plus 1 because C string uses one extra character in the end,
  ** the NULL ('\0') character. */

  while(*p!='\0')
  {
   *q++=*p++;
  }

  /* Until now, you've copied every character, except the NULL char at the end.
  ** you must set it in order for it to be a valid string. */
  *q = '\0';

  /* Now q points to the last (NULL) character of the string.
  ** In order to print it, you will need a pointer to the start of the string,
  ** that is why we need r: */
  printf("%s",r);

  /* When you are done using a memory buffer you allocated yourself,
  ** you must free it so it can be reused elsewhere. */
  free(r);

  getch();
}
于 2012-08-27T17:43:43.620 回答
2

a)我错在哪里?我的编程逻辑在哪里失败?

好吧,你做错了几件事。一是void main不规范;的返回类型main应该是int.

话虽如此,您正在寻找的真正问题与未初始化的事实有关q,但您仍试图通过它复制到内存(这是未定义的行为)。要纠正这个问题,请尝试分配q,例如

char *q = malloc(8);

请注意,您以后还必须注意free此处分配的内存。

除此之外,您也忘记了复制NUL终止符。

*q = 0;

...在您的复制循环之后。您还在增加指针后进行打印,因此在您调用q时它将不再位于字符串的开头。printf您应该将头部的副本存储在另一个变量中。此外,请小心使用printf没有任何换行符的纯文本,因为流可能会被缓冲,因此可能保持未刷新——使用显式fflush(stdout);


b)如何改进此代码以获得所需的输出?

好吧,我能想到的第一个最直接的改进是使用strcpy.

#include <stdio.h>

main() {
  const char p[] = "krishna";
  char q[sizeof p];
  strcpy(q, p);
  puts(q);
  getchar();
  return 0;
}
于 2012-08-27T17:51:23.523 回答
1

你不是说你想要 q 多少内存,所以,你试图超越其他东西的内存。因此,使用 malloc 或数组。

于 2012-08-27T17:33:05.410 回答
1

您需要'\0'在 q 的末尾放置一个字符。您还需要为其分配足够的内存。

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

int main()
{
  char *p="krishna";
  int size = strlen(p) + 1;
  char *q = (char *) malloc(size);
  char *qi = q;

  do
  {
   *q++=*p;
  } while(*p++!='\0');

  q = qi; //reset q to the beginning of the string

  printf("%s",q);
  getch();
  return 0;
}

我认为这应该有效

于 2012-08-27T17:33:48.103 回答
1

您正在使用 for 循环在末尾移动指针。此外,您也应该分配内存q

尝试这个:

void main()
{
 char *p="krishna";
 char *q = malloc(sizeof(char) * 20);
 char *temp = NULL;

 /* save q */
 temp = q;
  while(*p!='\0')
  {
   *q++=*p++;
  }
  /* reset saved q */
  q=temp;
  printf("%s",q);
  getch();
}
于 2012-08-27T17:34:27.317 回答
1

a)您移动q但不将其重置为字符串的开头。

正如许多其他人指出的那样,您还需要复制操作的目标是分配内存的某个地方,在堆上char *buf = malloc(BUF_SIZE)或在堆栈上char buf[BUF_SIZE]

最后,目的地必须是零终止的。

b)使用专用 API 通常比自制循环更具可读性和性能。

于 2012-08-27T17:34:30.513 回答
1

您应该为“q”分配内存以保存复制的字符串。

于 2012-08-27T17:31:53.287 回答
1

在您提供的这个示例中,您没有为新的字符数组分配内存,而是只是将字符复制到未知缓冲区中。这可能会导致各种问题(阅读有关缓冲区溢出和攻击的信息)。此外,一旦您完成将字符复制到未知缓冲区中,该缓冲区将不再指向字符串的开头,而是指向字符串的结尾。如果您的想法是将字符数组复制为 C 中的字符串,则可以使用以下代码执行此操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
  char *p="krishna";
  char *q = (char*) malloc(sizeof(char) * (strlen(p) + 1));

  strcpy(q, p);

  printf("%s", q);
  getchar();
  free(q);
  return 0;
}

在大多数情况下,NULL 字符 ('\0') 的数组末尾的额外字节是不必要的,但为了正确起见,它被包括在内,因为字符数组应该由 NULL 字符结束分隔。

通过使用 malloc,您可以为我们将字符串复制到的缓冲区分配(动态)内存。任何时候动态分配内存,它也应该在以后被释放,否则它将继续被不必要地使用。为了释放 malloc 的这种使用,在应用程序返回之前,最后使用了“free”命令。

于 2012-08-27T17:40:58.627 回答
0

正如许多人告诉我将 q 的值初始化为内存地址一样。我认为没有必要这样做。因为尽管存在未定义的行为,但这段代码仍能正常工作。

 int main()
 {
 char *p="krishna";
 char *q,*r;
 r=q;

  while(*p!='\0')
  {
   *q++=*p++;
  }
   *q='\0';
    q=r;
  printf("%s",q);
  getch();
  return 0;
 }
于 2012-08-27T18:39:12.537 回答
0

一项重要的改进建议,目前还没有(其他人已经指出了与字符串相关的问题)

Your declaration of main is not correct. 
The return value should  not be  void  but int
于 2012-08-27T17:46:24.357 回答