gpt4 book ai didi

c - token 之前的预期运算符错误

转载 作者:行者123 更新时间:2023-11-30 20:33:55 25 4
gpt4 key购买 nike

错误告诉我我在某个地方缺少一个运算符(operator),但我根本看不到它,所以我想一些新的眼睛可以帮助我找到它。

代码片段:

static int min_val, max_val;

struct arrNum
{
int charged;
int count;
};

static struct arrNum nums[];
static int max_num = 0;

static void sort_order(int iNum)
{
if (iNum < 0)
return;

if (iNum > max_num)
max_num = iNum;

struct arrNum nums[iNum].charged = 1;
struct arrNums nums[iNum].count++;

return;
}

错误:

mergeSort.c: In function 'sort_order':
mergeSort.c:32:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNum nums[iNum].charged = 1;
^
mergeSort.c:32:29: error: expected expression before '.' token
mergeSort.c:33:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNums nums[iNum].count++;
^
mergeSort.c:33:30: error: expected expression before '.' token

欢迎任何帮助。谢谢!

最佳答案

这是带有注释的代码版本:

// following two statements will cause the compiler to raise
// warning messages because this statements
// declare the variables, but they are never used
static int min_val;
static int max_val;

// define a struct with two fields
struct arrNum
{
int charged;
int count;
};

// declare an instance of a pointer to a struct
// actually want an array of `struct arrNum`
// so need a number between the '[' and ']'
// it must be given a size that is at least 1 greater
// than the highest value of 'iNum'
static struct arrNum nums[];

static int max_num = 0;

// following line will raise a compiler warning
// because 'static' function can only be referenced in the current file
// and nothing in the posted code calls it.
static void sort_order(int iNum)
{
if (iNum < 0)
return;

if (iNum > max_num)
max_num = iNum;

// note: both following statements are
// writing into 'la la land'
// because all that has been declared for
// 'nums[]' is a pointer
// and because it is declared 'static'
// is initialized to 0
// so executing either of these statements
// will result in a seg fault event
nums[iNum].charged = 1; // nums[] already declared, so don't declare it again
nums[iNum].count++; // nums[] already declared, so don't declare it again
}

关于c - token 之前的预期运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com