简体   繁体   English

使用结构时 C 中的 for 循环问题

[英]Issue with for loop in C when using struct

I did the following code to read values from a text file.我执行以下代码从文本文件中读取值。 Text file looks like this.(in code it's named as annotation.txt )文本文件看起来像这样。(在代码中它被命名为annotation.txt

299.jpg
480
640
3
dog
124
234
380
290
hand
789
456
234
123
wire
900
567
456
234

Here's the code.这是代码。

#include <stdio.h>
#include <stdlib.h>

typedef struct object
{
    char obj_name[20];
    int x_min;
    int y_min;
    int x_max;
    int y_max;

} Object;

typedef struct annotation
{
    char img_name[20];
    int width;
    int height;
    int num_obj;

    Object objects[];
} Annotation;

void readAnnotation(char *fileName, Annotation *annot);

int main()
{
    Annotation annot;
    readAnnotation("annotation.txt", &annot);

    printf("%s\n", annot.img_name);
    printf("%d\n", annot.width);
    printf("%d\n", annot.height);
    printf("%d\n", annot.num_obj);
    printf("0th object name for the 1st time %s\n", annot.objects[0].obj_name); // here it can be seen as 'dog'

    for (int i = 0; i < 3; i++)
    {
        printf("%s\n", annot.objects[i].obj_name); // oth one disappear
        printf("%d\n", annot.objects[i].x_min);
        printf("%d\n", annot.objects[i].y_min);
        printf("%d\n", annot.objects[i].x_max);
        printf("%d\n", annot.objects[i].y_max);
    };
    printf("0th object name for the 2nd time %s\n", annot.objects[0].obj_name); //here it disappear
};

void readAnnotation(char *fileName, Annotation *annot)
{
    FILE *fp;
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("\nError: Cannot open file");
        exit(1);
    };

    fscanf(fp, "%s", annot->img_name);
    fscanf(fp, "%d", &(annot->width));
    fscanf(fp, "%d", &(annot->height));
    fscanf(fp, "%d", &(annot->num_obj));

    for (int i = 0; i < annot->num_obj; i++)
    {
        fscanf(fp, "%s", annot->objects[i].obj_name);
        fscanf(fp, "%d", &(annot->objects[i].x_min));
        fscanf(fp, "%d", &(annot->objects[i].y_min));
        fscanf(fp, "%d", &(annot->objects[i].x_max));
        fscanf(fp, "%d", &(annot->objects[i].y_max));
    };

    fclose(fp);
};

When I run the code, inside the for loop written in main() function makes the annot.objects[0].obj_name to disappear.当我运行代码时,在 main() function 编写的 for 循环内使annot.objects[0].obj_name消失。 When I check it before the for loop the value can be seen as 'dog'.当我在 for 循环之前检查它时,该值可以被视为“狗”。 But within the for loop and after the for loop, that value disappears.但是在 for 循环内和 for 循环之后,该值消失了。 Can anyone tell me the issue with my code and give me a proper answer to solve this.谁能告诉我我的代码的问题并给我一个正确的答案来解决这个问题。

objects is never initialized. objects永远不会被初始化。 You're just writing to junk memory.您只是在写垃圾 memory。 Object objects[]; does not create an array, it actually creates a pointer to an array , and you're required to allocate memory for it.不创建数组,它实际上创建了一个指向数组的指针,并且您需要为其分配 memory 。 If you don't you're just exercising an uninitialized pointer and your program may or may not work or might crash.如果你不这样做,你只是在使用一个未初始化的指针,你的程序可能会也可能不会工作,或者可能会崩溃。 It's undefined behaviour .这是未定义的行为

Normally this is declared as Objects* objects to make it clear it's really a pointer and to remember to allocate.通常,这被声明为Objects* objects ,以明确它实际上是一个指针并记住要分配。 Arrays and pointers can often be interchanged, so no other code changes are required. Arrays 和指针通常可以互换,因此无需更改其他代码。

Before your readAnnotation loop:在您的readAnnotation循环之前:

annot->objects = calloc(annot->num_obj, sizeof(Object));

regarding:关于:

typedef struct annotation
{
    char img_name[20];
    int width;
    int height;
    int num_obj;

    Object objects[];
} Annotation;

The above only allocates room for the first 4 fields then a pointer, however, No room is allocated for an array 0f object Suggest: either use malloc() and/or realloc() to allocate memory for the instances of Object上面只为前 4 个字段分配空间,然后是一个指针,但是,没有为数组 0f object分配空间建议:使用malloc()和/或realloc()为 Z49754317994151AAC35 的实例分配Object

Otherwise any reference to objects[x] is undefined behavior can can lead to a seg fault event否则,任何对objects[x]的引用都是未定义的行为可能导致段错误事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM