|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- struct Data
- {
- char* str;
- struct Data* next;
- };
- int main(void)
- {
- struct Data* d = malloc(sizeof(struct Data));
- gets(d->str);
- printf("%s\n", d->str);
- return 0;
- }
复制代码
运行时报错:0x7A21FA2C (ucrtbased.dll)处(位于 tiaoshi1.exe 中)引发的异常: 0xC0000005: 写入位置 0xCDCDCDCD 时发生访问冲突。
我知道是因为我没有给str内存空间导致无法写入,我想知道是否有其他方法不需要给空间直接能输入的函数(不想在结构体中定义数组)【我胡思乱想的问题,自己找函数文档并没有发现可以直接给空间的函数,所以想寻求各位大佬是否有那种奇妙的想法 】
本帖最后由 jhq999 于 2022-12-30 21:59 编辑
原来是需要函数,试试自己写
c++里的类能够解决
- #include <stdio.h>
- #include <stdlib.h>
- struct Data
- {
- char* str;
- struct Data* next;
- };
- struct Data *structdate()
- {
- struct Data* d = malloc(sizeof(struct Data));
- d->str=(char*)malloc(256);
- return d;
- }
- void decdata(struct Data * d)
- {
- if(d->str)free(d->str);
- free(d);
- }
- int main(void)
- {
- struct Data* d = structdate();
- gets(d->str);
- printf("%s\n", d->str);
- decdata(d);
- return 0;
- }
复制代码
|
|