c - 错误 : expected ‘:’ , ‘,’、 ‘;’、 ‘}’ 或 ‘__attribute__’ token 之前的 ‘=’

标签 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
};

如有任何见解,我们将不胜感激!

最佳答案

问题出在

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 变量。

关于c - 错误 : expected ‘:’ , ‘,’、 ‘;’、 ‘}’ 或 ‘__attribute__’ token 之前的 ‘=’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654347/

相关文章:

C 原型(prototype)范围

c - 在执行procprob时,b可以是任何数据类型吗?

pointers - Go结构指针不是唯一的

c - C 中的 XOR 相同数据导致非零值。为什么?

c - 任务系统问题 - 任务将运行两次(重新发布一次)

c - 递归难度

c++ - 性能 : better to inizialize variable or just fill it?

确保元素位于结构开头的 C 宏

c - 如何模拟 pam_handle 结构

c - fscanf 和链表的 char *