c - 为什么说 "expression is not assignable"?

标签 c kernighan-and-ritchie

在开始之前,我只想澄清一下,我知道错误的含义,只是不知道为什么会这样。这不仅来自著名的书,而且我不确定为什么 pointer = pointer 在这里是非法的。不管怎样,这里是来自 K&R 的代码,在声明和其他内容的顺序上做了一些细微的改动,但大部分都是完整的:

#include <stdio.h>
#include <string.h>

#define MAXLINES 5000     /* max #lines to be sorted */
#define MAXLEN 1000  /* max length of any input line */

extern char *alloc(int);
int igetline(char *, int);
char *lineptr[MAXLINES];  /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);

/* sort input lines */
int main(void)
{
    int nlines;     /* number of input lines read */

    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
        qsort(lineptr, 0, nlines-1);
        writelines(lineptr, nlines);
        return 0;
    } else {
        printf("error: input too big to sort\n");
        return 1;
    }
}

/* readlines: read input lines */
int readlines(char *lineptr[], int maxlines)
{
    int len, nlines;
    char *p, line[MAXLEN];

    nlines = 0;
    while ((len = igetline(line, MAXLEN)) > 0)
        if (nlines >= maxlines || p = alloc(len) == NULL)
            return -1;
        else {
            line[len-1] = '\0';  /* delete newline */
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    return nlines;
}

/* writelines: write output lines */
void writelines(char *lineptr[], int nlines)
{
    int i;

    for (i = 0; i < nlines; i++)
        printf("%s\n", lineptr[i]);
}

/* igetline: read a line into s, return length */
int igetline(char s[], int lim)
{
    int c, i;

    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

/* qsort: sort v[left]...v[right] into increasing order */
void qsort(char *v[], int left, int right)
{
    int i, last;
    void swap(char *v[], int i, int j);

    if (left >= right)  /* do nothing if array contains */
        return;         /* fewer than two elements */
    swap(v, left, (left + right)/2);
    last = left;
    for (i = left+1; i <= right; i++)
        if (strcmp(v[i], v[left]) < 0)
            swap(v, ++last, i);
    swap(v, left, last);
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}

/* swap: interchange v[i] and v[j] */
void swap(char *v[], int i, int j)
{
    char *temp;

    temp = v[j];
    v[i] = v[j];
    v[j] = temp;
}

请注意 alloc() 在另一个文件中。我不认为这与此有任何关系,但无论如何这是代码:

#define ALLOCSIZE 10000 /* size of available space */

static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf;  /* next free position */

char *alloc(int n) /* return pointer to n characters */
{
    if (allocbuf + ALLOCSIZE - allocp >= n) {
        allocp += n;
        return allocp - n;
    } else
        return 0;
}

void afree(char *p)   /* free storage pointed to by p */
{
    if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
        allocp = p;
}

当我尝试编译上面的代码时,它给了我这个错误:

cc sample5.6.c alloc.c
sample5.6.c:37:31: error: expression is not assignable
                if (nlines >= maxlines || p = alloc(len) == NULL)
                    ~~~~~~~~~~~~~~~~~~~~~~~ ^
1 error generated.

最佳答案

if (nlines >= maxlines || p = alloc(len) == NULL)

只要简单看一下语言语法,就会很明显 if () 中的内容语句是一个赋值表达式。换句话说,它被解析为:

if ( <b>(</b>nlines >= maxlines || p<b>)</b> = <b>(</b>alloc(len) == NULL<b>)</b> )

您可能想要的是:

if (nlines >= maxlines || <b>(</b>p = alloc(len)<b>)</b> == NULL)

这与我的 K&R2 副本中的代码相同。

语言语法

以下是 n1570 的语言语法部分的摘录(当前 C 标准的最终公开草案),第 467 和 472 页(严重缩写)。 K&R2(Kernighan 和 Ritchie 的“C 程序设计语言,第二版”)中详述的语法相同

selection-statement:
      if ( expression ) statement

expression:
      assignment-expression

assignment-expression:
      conditional-expression
      unary-expression assignment-operator assignment-expression

conditional-expression:
      logical-OR-expression

logical-OR-expression:
      logical-OR-expression || logical-AND-expression

关于c - 为什么说 "expression is not assignable"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419886/

相关文章:

c - 事件驱动与使用返回值的调用者相比

c - 我需要在数组中存储一些整数并用字母符号打印它们

c - ## 预处理器运算符的应用和需要考虑的陷阱是什么?

c - 在C中随机打印一个字符

c++ - 我是否应该始终声明比我的字符串大的 char 数组?

c - 用c画正弦波?

c - getop() 函数 K&R 书第 78 页

c - 二叉树元素的递归 printf

c - 函数声明与原型(prototype)的替代 (K&R) C 语法

c - 函数声明与原型(prototype)的替代 (K&R) C 语法