鱼C论坛

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

[已解决]关于给结构体中字符串指针赋值的问题?

[复制链接]
发表于 2022-12-30 21:36:03 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Data
  4. {
  5.         char* str;
  6.         struct Data* next;
  7. };

  8. int main(void)
  9. {
  10.         struct Data* d = malloc(sizeof(struct Data));
  11.         gets(d->str);
  12.         printf("%s\n", d->str);

  13.         return 0;
  14. }
复制代码


运行时报错:0x7A21FA2C (ucrtbased.dll)处(位于 tiaoshi1.exe 中)引发的异常: 0xC0000005: 写入位置 0xCDCDCDCD 时发生访问冲突。

我知道是因为我没有给str内存空间导致无法写入,我想知道是否有其他方法不需要给空间直接能输入的函数(不想在结构体中定义数组)【我胡思乱想的问题,自己找函数文档并没有发现可以直接给空间的函数,所以想寻求各位大佬是否有那种奇妙的想法
最佳答案
2022-12-30 21:41:00
本帖最后由 jhq999 于 2022-12-30 21:59 编辑

原来是需要函数,试试自己写
c++里的类能够解决
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Data
  4. {
  5.         char* str;
  6.         struct Data* next;
  7. };
  8. struct Data *structdate()
  9. {
  10.     struct Data* d = malloc(sizeof(struct Data));
  11.     d->str=(char*)malloc(256);
  12.     return d;
  13. }
  14. void decdata(struct Data * d)
  15. {
  16.     if(d->str)free(d->str);
  17.     free(d);
  18. }
  19. int main(void)
  20. {
  21.         struct Data* d = structdate();
  22.         gets(d->str);
  23.         printf("%s\n", d->str);
  24.         decdata(d);
  25.         return 0;
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-30 21:41:00 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-12-30 21:59 编辑

原来是需要函数,试试自己写
c++里的类能够解决
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Data
  4. {
  5.         char* str;
  6.         struct Data* next;
  7. };
  8. struct Data *structdate()
  9. {
  10.     struct Data* d = malloc(sizeof(struct Data));
  11.     d->str=(char*)malloc(256);
  12.     return d;
  13. }
  14. void decdata(struct Data * d)
  15. {
  16.     if(d->str)free(d->str);
  17.     free(d);
  18. }
  19. int main(void)
  20. {
  21.         struct Data* d = structdate();
  22.         gets(d->str);
  23.         printf("%s\n", d->str);
  24.         decdata(d);
  25.         return 0;
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-30 21:49:48 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Data
  4. {
  5.     char* str;
  6.     struct Data* next;
  7. };

  8. int main(void)
  9. {
  10.     struct Data* d = (struct Data*)malloc(sizeof(struct Data));
  11.     char str[7]; // 另外创建一个新的字符指针
  12.     scanf("%s", str); // 赋值
  13.     d->str = str; // 指针指向 str
  14.     printf("-> %s\n", d->str); // 打印
  15.     return 0;
  16. }
复制代码
  1. FishC
  2. -> FishC
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-30 22:29:42 | 显示全部楼层
jhq999 发表于 2022-12-30 21:41
原来是需要函数,试试自己写
c++里的类能够解决

太棒了,我没有想到可以自己new一个出来,还是大佬强!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-30 22:48:50 | 显示全部楼层
  1. struct Data
  2. {
  3.         char* str;
  4.         struct Data* next;
  5. };
复制代码

        这样定义的 str 只是一个指针,只能用来指向一个储存了字符串的内存地址,而结构体 struct Data 却并不能用于储存字符串,如果要彻底解决问题,必须这样定义 str 成员:
  1. struct Data
  2. {
  3.         char str[256] ;
  4.         struct Data* next ;
  5. };
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 15:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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