c - 复制指向结构的指针在c中不起作用
问题描述
我正在学习 C 中的指针,但在将指针复制到结构时遇到了问题。
****完整代码:https ://1drv.ms/t/s!Av5xYBEjrdmuqS1sfgGB7pFCxGeu
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cal {
int date;
int time;
int importance;
char group[256];
char title[256];
char description[256];
};
long long int count = 0;
int main() {
FILE *file;
......
int *group;
group = (int *)malloc(sizeof(int) * count);
group = (int *)calloc(count, sizeof(int));
struct cal *calendar;
calendar = (struct cal *)malloc(sizeof(struct cal)*count);
calendar = (struct cal *)calloc(count, sizeof(struct cal));
for (int i = 0; i < count; i++) {
char *ptr = strtok(arrs[i], " ");
while (ptr != NULL) {
calendar[i].date = 'ptr';
ptr = strtok(NULL, " "); //date
calendar[i].time = 'ptr';
ptr = strtok(NULL, " "); //time
calendar[i].importance = 'ptr';
ptr = strtok(NULL, " "); //importance
*calendar[i].group = 'ptr';
ptr = strtok(NULL, " ");//group END
*calendar[i].title = 'ptr';
ptr = strtok(NULL, " ");//Title
*calendar[i].description = 'ptr';
ptr = strtok(NULL, " ");//Discription
}
}
for(int i=0;i<count;i++)
printf("%d %d %d %s %s %s\n", calendar[i].date, calendar[i].time,
calendar[i].importance, calendar[i].group, calendar[i].title,
calendar[i].description);
}
输入必须在 input.txt 上。像这样:
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription] (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription] (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription] (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription] (eof)
输出必须是组 0-4 中相同组的总和。例如)1 0 1 2 0
在里面
for (int i = 0; i < count; i++) {
.....
}
部分不工作。指针 'ptr' 不会复制到 'calendar[i].somthing'。我试过使用 calendar[i].date = '*ptr'; 但它也不起作用。
解决方案
Park,您可能需要清理代码的这些部分,并可能使用新代码进行编辑,以明确您要完成的工作。首先,让我们看看动态分配内存时的职责,
没有必要强制返回malloc
,这是不必要的。请参阅:我是否强制转换 malloc 的结果?
在您编写的任何动态分配内存的代码中,您对分配的任何内存块都有两个责任:(1) 始终为您分配的每个内存块保留一个指向起始地址的指针, (2)它可以在它被释放时被释放不再需要。
int *group;
group = (int *)malloc(sizeof(int) * count);
group = (int *)calloc(count, sizeof(int));
您是否保留了指向已分配的每个块的指针?(提示:否)。在您第一次分配 之后group
,它会保存您分配的块中的第一个地址malloc
。到目前为止一切正常。
然后你打电话calloc
给 分配一个新地址group
。持有的旧地址怎么了group
?您需要保留以确保可以释放不再需要的内存块。你将如何取回那个地址(提示:你不能让它消失......这就是所谓的内存泄漏。
如果您正在进行第二次调用,calloc
因为您希望分配的块中的所有字节都初始化为零,那么不要使用malloc
,只需使用一次调用calloc
. malloc
两者和calloc
同一个指针从来没有任何理由- 没有。
你对calendar
. 因为你做同样的事情,同样的坏结果也会随之而来。
所有你需要的是:
group = calloc (count, sizeof *group);
进而
calendar = calloc (count, sizeof *calendar);
malloc
这一切都引出了一个问题——你为什么要进行分配calloc
?您可以简单地创建一个具有自动存储功能的整数数组,并以相同的方式创建一个日历实例。(无需分配)。在这一点上,这对你来说似乎是一个更好的方法。动态分配并没有什么问题,您会经常使用它,但考虑到您目前对该概念的困难,您可能希望稍后再使用它,并简单地使用为数组和结构提供的自动存储。(这更有意义,因为您没有calendar
需要进一步分配的内容)
最后,随着您使用strtok
(或您使用的任何对程序输入至关重要的函数)——您必须在每次调用它时检查返回,否则您无法确信您正在处理实际数据(而不是垃圾) 在您的代码中。
推荐阅读
- python - 如何将具有numpy数组值的熊猫系列转换为数据框
- makefile - 没有规则来制作目标*,*需要
- java - 单元测试 Spring 抛出 BeanNotOfRequiredTypeException
- python - 如何优化下面的代码以更快地运行,我的数据框大小几乎是 100,000 个数据点
- android - 在 android studio 中将实时数据库添加到我的应用程序 - 无法解决:firebase-database-15.0.0
- laravel - 十月CMS后台登录
- c++ - 使用 2 个指针反转链表
- python - Python - 比较包含的两个字典值并显示匹配项
- c++ - C++ OpenCV 进行人脸识别,直到相机被移除
- javascript - 样式属性不适用于带后缀或前缀的输入