简体   繁体   English

C中的结构数组

[英]Structures array in C

I am still learning C, despite some working programs, but now I came to structures and the pains started. 我仍在学习C,尽管有一些工作程序,但现在我来到结构并且痛苦开始了。 I have an already working program, in XC8 under MPLABX (target is a PIC18 ), composed by a Main.c , an header and some additional .c files. 我有一个已经工作的程序,在XC8下的MPLABX (目标是PIC18 ),由Main.c ,一个头和一些额外的.c文件组成。 In the header I already have an array of structures (font descriptors), perfectly working. 在标题中我已经有一个结构数组(字体描述符),完美的工作。

Now, I want to add a new piece of code (dynamic menus) so I made an additional header file (struct.h) with the structure and array definition, plus a new C file (menus.c) that will handle the menus; 现在,我想添加一段新的代码(动态菜单),所以我创建了一个带有结构和数组定义的附加头文件(struct.h),以及一个处理菜单的新C文件(menus.c); both files are included in the main.c 这两个文件都包含在main.c中

When I started to write the menu's rows assignements I got the following errors: 当我开始编写菜单的行分配时,我收到以下错误:

invalid dimension 无效的维度

and then 接着

missing basic type; 缺少基本类型; int assumed int假设

These are the (very simplified) pieces of offending codes: 这些是(非常简化的)违规代码:

STRUCT.h STRUCT.h

#ifndef STRUCT_H
#define STRUCT_H

typedef struct Row
{
    char *label;
    int posX;
    int posY;
};

struct Row rows[20];

#endif  /* STRUCT_H */

MENUS.c MENUS.c

#ifndef STRUCT_H
#include  "struct.h"
#endif

rows[0].posX = 1;

The error rises at this last row. 错误在最后一行上升。 I'm 100% sure this is a mine very stupid error, but where? 我百分百肯定这是我非常愚蠢的错误,但在哪里? Thanks 谢谢

First of all it's not necessary to protect the include from main since it's already done from STRUCT.h 首先,没有必要保护include和main,因为它已经从STRUCT.h完成了

So instead of in main 所以而不是主要的

#ifndef STRUCT_H
#include  "struct.h"
#endif

directly use(in main the .h is the one which should have protection which it already does, no need to do it twice) 直接使用(在主要的.h是应该有保护的那个,它已经做了,不需要做两次)

#include  "struct.h"

Now back to your code the problem you are having is that you're not modifying the structure in a function, meaning that rows[0].posX = 1; 现在回到你的代码,你遇到的问题是你没有修改函数中的结构,这意味着rows [0] .posX = 1; will never be executed 将永远不会被执行

This should work: 这应该工作:

#include "struct.h"

int main(void)
{

    rows[0].posX = 1;

    //do something else like print result etc
    return 0;
}

Also you're not using the typedef, it's an empty use of typedef since you didn't put any name afterwords note these two are the same: 你也没有使用typedef,它是一个空的typedef使用,因为你没有放任何名字后注意这两个是相同的:

typedef struct Row
{
    char *label;
    int posX;
    int posY;
};



struct Row
{
    char *label;
    int posX;
    int posY;
};

I would use something along the lines of 我会用一些东西

typedef struct Row
{
    char *label;
    int posX;
    int posY;
}row_t;

Now you can directly use row_t as a data type instead of struct row 现在您可以直接使用row_t作为数据类型而不是struct行

Finally I wouldn't make it a global variable I would make it local to main and pass a pointer to any function that requieres the array/membery of array 最后我不会把它变成一个全局变量我会把它变成main的本地变量并传递指向任何需要数组的数组/成员的函数的指针

#include <stdio.h>

typedef struct Row
{
    char *label;
    int posX;
    int posY;
}row_t;


int main(void)
{

    row_t rows[20];//Not global local to main
    rows[0].posX = 1;
    printf("%d\n",rows[0].posX );
    //do something else like print result etc
    return 0;
}

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

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