鱼C论坛

 找回密码
 立即注册
查看: 867|回复: 3

[已解决]void指针问题

[复制链接]
发表于 2020-4-14 18:18:45 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main(void)
  4. {
  5.    char src[] = "******************************";
  6.    char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
  7.    char *ptr;
  8.    printf("destination before memcpy: %s\n", dest);
  9.    ptr = memcpy(dest, src, strlen(src));
  10.    if (ptr)
  11.       printf("destination after memcpy:  %s\n", dest);
  12.    else
  13.       printf("memcpy failed\n");
  14.    return 0;
  15. }
复制代码

这段代码报错的原因是什么啊,
最佳答案
2020-4-14 22:30:01
  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6.         char src[] = "******************************";
  7.         char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
  8.         char *ptr;
  9.         printf("destination before memcpy: %s\n", dest);
  10.         ptr =(char*) memcpy(dest, src, strlen(src));
  11.         if (ptr)
  12.                 printf("destination after memcpy:  %s\n", dest);
  13.         else
  14.                 printf("memcpy failed\n");
  15.         return 0;
  16. }
复制代码

这个意思吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-14 18:23:07 | 显示全部楼层
我这里正常
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 22:30:01 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6.         char src[] = "******************************";
  7.         char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
  8.         char *ptr;
  9.         printf("destination before memcpy: %s\n", dest);
  10.         ptr =(char*) memcpy(dest, src, strlen(src));
  11.         if (ptr)
  12.                 printf("destination after memcpy:  %s\n", dest);
  13.         else
  14.                 printf("memcpy failed\n");
  15.         return 0;
  16. }
复制代码

这个意思吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-4-14 22:52:27 | 显示全部楼层
memcpy的返回值类型是void *,赋值时类型要匹配,否则编译报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 13:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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