将指针复制到不在c中工作的结构

时间:2018-06-03 07:48:06

标签: c pointers

我在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组中相同组的总和。 ex)1 0 1 2 0

中的

for (int i = 0; i < count; i++) {
            .....
        }

部分不起作用。指针&#39; ptr&#39;不会复制到&#39; calendar [i] .somthing&#39;。 我尝试过使用日历[i]。日期=&#39; * ptr&#39 ;;但它也无法正常工作。

1 个答案:

答案 0 :(得分:0)

Park,您可能需要清理代码的这些部分,并可能使用新代码进行编辑,以明确您要完成的任务。首先,让我们看一下动态分配内存时的责任,

没有必要强制转换malloc,这是不必要的。请参阅:Do I cast the result of malloc?

在您编写的任何动态分配内存的代码中,对于分配的任何内存块,您有2个职责(1) 始终保留指向您分配的每个内存块的起始地址(2)在不再需要时可以释放

int *group;
group = (int *)malloc(sizeof(int) * count);
group = (int *)calloc(count, sizeof(int));

您是否保留了指向已分配的每个块的指针? (提示:否)。在您第一次分配group后,它会保留您使用malloc分配的块中的第一个地址。到目前为止一切都还可以。

然后,您致电callocgroup分配地址。 group所持的旧地址怎么了?您需要保留的那个,以确保不再需要释放内存块。你将如何获得该地址(提示:它不能消失......这就是所谓的内存泄漏

如果您使用calloc进行第二次通话,因为您希望所分配的块中的所有字节都已初始化为零,那么请不要使用malloc,只需使用一次调用{ {1}}。 callocmalloc同样的指针 - 没有任何理由也没有任何理由。

您使用calloc执行完全相同的操作。既然你做了同样的事情,那么结果就会出现同样糟糕的结果。

您只需要:

calendar

然后

group = calloc (count, sizeof *group);

这一切都引出了一个问题 - 为什么要分配calendar = calloc (count, sizeof *calendar); malloc?您可以使用自动存储创建一个整数 Array ,并以相同的方式创建日历实例。 (不需要分配)。对于你来说,这似乎是一个更好的方法。动态分配没有任何问题,你会使用它很多,但鉴于你目前在概念上遇到的困难,你可能希望稍后将其保留一段时间,只需使用为数组和结构提供的自动存储。 (考虑到calloc内没有任何需要进一步分配的内容),这就更有意义了

最后,使用calendar(或您使用的任何对您的程序输入至关重要的功能) - 每次调用时都必须检查返回,否则您可以毫不自信地在代码中处理实际数据(而不是垃圾)。