错误:在'='标记之前预期':',',',';','}'或'__attribute__'

时间:2018-04-04 15:00:56

标签: c struct

为什么会出现此错误(对于int index = -1)。这是代码段:

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

#define Offset 12

int num_frames;

struct node
    {
        struct node* next;
        long data;
    };

struct page
{
    int index = -1;
    int frame = -1;
    int dirty = 0; //0 is clean, 1 is dirty
    int valid = 0; //0 is not, 1 is valid
    int referenced = 0; //0 is not, 1 is referenced
};

任何见解都将受到赞赏!

1 个答案:

答案 0 :(得分:2)

问题在于

    A  B  C #in a line

您定义了一个新类型struct page { int index = -1; int frame = -1; int dirty = 0; //0 is clean, 1 is dirty int valid = 0; //0 is not, 1 is valid int referenced = 0; //0 is not, 1 is referenced }; 并尝试在同一声明中初始化它。类型没有默认值。正确的方法是定义没有初始值的新类型struct page,然后使用这些值启动新的struct page变量。