令牌之前的预期运算符出错

时间:2017-04-29 00:43:40

标签: c

错误告诉我,我在某个地方错过了一个操作员,但我根本看不到它,所以我觉得一些新鲜的眼睛可以帮我找到它。

代码段:

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

欢迎任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

这是带注释的代码版本:

// 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
}