【问题标题】:In C assigning value to a field inside structure is resulting in "expected a ';'" error在 C 中,将值分配给结构内的字段会导致“预期为 ';'”错误
【发布时间】:2021-01-12 12:54:25
【问题描述】:

我已将值分配给结构中的字段。这导致了多个错误。

typedef struct t_queue {
int head = 0;
int tail = 0;
int maxSize = 0;
int size = 0;
SOCKET* queue = NULL;
}Queue;

typedef struct t_threadData {
int topicID;

bool isEngineActive = FALSE;
bool isServerActive = TRUE;
int numberOfConnectedSubs = 0;

Queue* queue;

HANDLE PublisherReady;
HANDLE ThreadReady;
HANDLE BarrierOK;
CRITICAL_SECTION Critical_Section;

int NumberOfThreadsWaiting = 0;
SOCKET sockets[NUMBER_OF_SUBSCRIBERS];

}ThreadData;

错误是:

  1. E0065 需要一个 ';'

  2. E0020 标识符“bool”未定义

我正在使用 Visual Studio Enterprise 2017 在 Windows 10 上使用 C 语言工作。

提前感谢您的帮助。

【问题讨论】:

标签: c windows structure


【解决方案1】:

阅读Modern C 并查看this C reference。如果您使用最新的GCC 编译器进行编译,请使用gcc -Wall -Wextra -g 编译您的代码。另请阅读 C 编译器的文档,并启用其中的所有警告。另请阅读调试器的文档(例如 GDB

你应该编码:

typedef struct t_queue {
  int head;
  int tail;
  int maxSize;
  int size;
  SOCKET* queue;
}  Queue;

顺便说一句,您的字段名称令人困惑。因为headtail 建议使用doubly-linked list。那么它们应该是指针!

考虑在您的项目中使用Clang static analyzer - 或Frama-C-。

从一些用 C 编码的现有开源软件的源代码中汲取灵感(如 GlibGNU make、Linux kernelGNU guile 等...)

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2015-08-06
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多