简体   繁体   English

我如何将其变成结构?

[英]How do i make this into a struct?

OK so I have erased everything trying to make this into a struct, because i messed it up bad. 好的,所以我已经删除了所有试图将其变成结构的东西,因为我把它弄糟了。

I need this code of arrays to become a struct. 我需要这个数组代码成为一个结构。

FILE *pFile;
    int choice = 0;
    char buf[40];


    int id[sizeof(buf)];
    char name[sizeof(buf)][20];
    char state[sizeof(buf)][5];
    char dis_code[sizeof(buf)];
    float balance[sizeof(buf)];
    char due_date[sizeof(buf)][40];

This is what I got do so far but when i try to use it it goes nuts. 这是我到目前为止所做的事情,但是当我尝试使用它时,它变得疯狂。 I still don't know how to load the file into it. 我仍然不知道如何将文件加载到其中。

struct fileinfo
{
    int id[10];
    char name[20];
    char state[5];
    char dis_code[5];
    float balance[10];
    char due_date[40];
} info[sizeof(buf)];

Am i missing something or do i have the right idea. 我错过了什么或者我有正确的想法。 The problem is when i run this the same why i would the regular arrays I run into errors. 问题是当我运行它时,为什么我会遇到常规数组错误。

I'm not sure if this is your only issue, but you have changed the type of several of the fields. 我不确定这是否是您唯一的问题,但您已经更改了几个字段的类型。

int id[sizeof(buf)];        // id[i] is an int
char dis_code[sizeof(buf)]; // dis_code[i] is a char
float balance[sizeof(buf)]; // balance[i] is a float

struct fileinfo
{
    int id[10];             // info[i].id is an _array of 10 ints_
    char dis_code[5];       // info[i].dis_code is an _array of 5 chars_
    float balance[10];      // info[i].balance is an _array of 10 floats_
} info[sizeof(buf)];

An array of a type and single instance of that type will behave quite differently. 类型和该类型的单个实例的数组将表现完全不同。

I would suggest making the fields of the struct the same type as your original array elements, ie: 我建议使结构的字段与原始数组元素的类型相同,即:

struct fileinfo
{
    int id;
    char dis_code;
    float balance;
}

Since you declared char buf[40] ==> sizeof(buf) = 40 . 因为你声明了char buf[40] ==> sizeof(buf) = 40 You are changing the 2 dimensional array into a single dimensional. 您正在将二维数组更改为一维。 The structure should be like 结构应该是这样的

   struct fileinfo
   {             
     int id[40];
     char name[40][20];
     char state[40][5];
     char dis_code[40];
     float balance[40];
     char due_date[40][40];
    }

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

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