鱼C论坛

 找回密码
 立即注册
查看: 1925|回复: 4

[已解决]C语言动态内存打印问题

[复制链接]
发表于 2023-11-15 16:42:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个代码是运用动态内存知识实现对输入的字符串反向打印的功能,请问为什么我打印出来之后永远多一个% 在后面呀 一脸新手懵逼
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"

  4. int countp = 0;
  5. char *get_reverse(void);
  6. void printc(char *p, int count);

  7. int main(void)
  8. {
  9.     char *p = NULL;
  10.     p = get_reverse();
  11.     printc(p, countp);

  12.     return 0;
  13. }

  14. char *get_reverse(void)
  15. {
  16.     char ch;
  17.     char *p = NULL;
  18.     char *dp = NULL;
  19.     int count = 0, i = 0;

  20.     printf("Please enter char:");
  21.     while((ch = getchar()) != '\n')
  22.     {
  23.         count++;
  24.         p = (char *)realloc(p, count);
  25.         p[count - 1] = ch;
  26.     }


  27.     dp = (char *)malloc(count + 1);
  28.     dp[count] = '\0';

  29.     countp = count;

  30.     for(i = 0; count > 0; i++, count--)
  31.     {
  32.         dp[i] = p[count - 1];
  33.     }

  34.     free(p);
  35.     return dp;
  36. }

  37. void printc(char *p, int count)
  38. {
  39.     int i;
  40.     printf("Reversed char is:");
  41.     for(i = 0; i < count; i++)
  42.     {
  43.         printf("%c", p[i]);
  44.     }
  45.     putchar("\n");
  46. }
复制代码
最佳答案
2023-11-15 21:16:02
putchar 函数用于输出单个字符,而不是字符串。

在C语言中,字符串是以 '\0' (空字符)结尾的字符数组

使用 putchar("\n") 时,编译器会给出一个警告,因为试图将字符串而不是单个字符传递给 putchar。

正确的方式是使用 putchar('\n') 来输出一个换行符,或者您可以使用 printf("\n")。

  1. void printc(char *p, int count)
  2. {
  3.     int i;
  4.     printf("Reversed char is:");
  5.     for(i = 0; i < count; i++)
  6.     {
  7.         printf("%c", p[i]);
  8.     }
  9.     printf("\n"); // 或者 putchar('\n');
  10. }
复制代码

捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-15 18:22:28 | 显示全部楼层
dp = (char *)malloc(count+1); -》 dp = (char *)malloc(count);


putchar('\n');

你是按字符数输出,反向后也没有加'\0'。倒是接收时加了'\0'。不理解何意,也就是说你接收时也可以不用加'\0'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-15 21:16:02 | 显示全部楼层    本楼为最佳答案   
putchar 函数用于输出单个字符,而不是字符串。

在C语言中,字符串是以 '\0' (空字符)结尾的字符数组

使用 putchar("\n") 时,编译器会给出一个警告,因为试图将字符串而不是单个字符传递给 putchar。

正确的方式是使用 putchar('\n') 来输出一个换行符,或者您可以使用 printf("\n")。

  1. void printc(char *p, int count)
  2. {
  3.     int i;
  4.     printf("Reversed char is:");
  5.     for(i = 0; i < count; i++)
  6.     {
  7.         printf("%c", p[i]);
  8.     }
  9.     printf("\n"); // 或者 putchar('\n');
  10. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-16 08:26:15 | 显示全部楼层
ba21 发表于 2023-11-15 18:22
dp = (char *)malloc(count+1); -》 dp = (char *)malloc(count);

谢谢老哥指正的其他问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-16 08:26:44 | 显示全部楼层
不二如是 发表于 2023-11-15 21:16
putchar 函数用于输出单个字符,而不是字符串。

在C语言中,字符串是以 '\0' (空字符)结尾的字符数组
...

谢谢不二师兄一语点破,果然改完就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 00:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表