【问题标题】:error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token [duplicate]错误:在“=”标记之前应有“:”、“,”、“;”、“}”或“__attribute__” [重复]
【发布时间】:2018-04-04 15:00:56
【问题描述】:

为什么会弹出此错误(对于 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
};

任何见解将不胜感激!

【问题讨论】:

  • 您不能在声明中初始化结构成员。该语法(可能有用)不存在

标签: c struct


【解决方案1】:

问题出在

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,然后使用这些值启动一个新的struct page 变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多