C 中不兼容的枚举类型

发布于 2024-12-07 11:24:26 字数 389 浏览 0 评论 0原文

我有一个关于枚举和数组的问题。本质上,我有一个枚举“BIT”数组,声明为枚举类型“word”。

typedef enum {
ZERO = (uint8_t) 0, ONE = (uint8_t) 1
} BIT;

typedef BIT word[16];

正如向我解释的那样,“单词”只是一个预定义的 16 位数组。但是,当我尝试分配给已声明的单词时,我只是收到一条错误消息,指出类型单词和位不兼容。

BIT ten = ZERO;
word Bob;
Bob[10] = ten;

我只能用另一个单词写入单词 Bob 吗?我会想,因为“单词”是一个“BIT”数组,所以我可以简单地将一个位分配给“单词”数组中的一个位置。

I have a question about enums and arrays. Essentially I have an array of enum "BIT"s declared as an enum type "word".

typedef enum {
ZERO = (uint8_t) 0, ONE = (uint8_t) 1
} BIT;

typedef BIT word[16];

As it was explained to me, "word" is simply a predefined array of 16 BITs. However, when I try to assign to a declared word, I simply get an error saying incompatible type word and BIT.

BIT ten = ZERO;
word Bob;
Bob[10] = ten;

Can I only write to the word Bob with another word, I would have thought since "word" is an array of "BIT"s that I could simply assign a bit to a position in the "word" array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2024-12-14 11:24:26

上下文

问题的一个版本包含代码:

源文件logic.c

<前><代码>#include“logic.h”
无效word_not(字* R,字* A)
{
for (int i = 0; i<16; i++)
{
如果 ((A+i))
{*(R+i) = 0;}
别的
{*(R+i) = 1; }
}
}

头文件logic.h

<前><代码>#ifndef LOGIC_H_
#定义LOGIC_H_

#include;

/**
* LC-3 机器的基本元件。
*
* BIT 的值为 ZER0 (0) 或 ONE (1)
*/
typedef 枚举 {
零 = (uint8_t) 0,一 = (uint8_t) 1
} 少量;

/**
* 大端格式的 16-“BIT”LC-3 字
*/
typedef 位字[16];

无效word_not(字* R,字* A);

#endif


问题说明问题

在于word_not() 的参数是指向数组的指针。内部的符号必须相应调整:

void word_not(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
    {
        if ((*A)[i])
            (*R)[i] = ZERO;
        else
            (*R)[i] = ONE;
    }
}

尽管您可以更简洁地写为:

void word_not(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
        (*R)[i] = !(*A)[i];
}

或者,您可以简单地将函数重新定义为:

void word_not(word R, word A)
{
    for (int i = 0; i < 16; i++)
    {
        if (A[i])
            R[i] = ZERO;
        else
            R[i] = ONE;
    }
}

或者,再次更简洁地,为:

void word_not(word R, word A)
{
    for (int i = 0; i < 16; i++)
        R[i] = !A[i];
}

可编译测试用例 - 输出

0: 1 1 1 1
1: 0 0 0 0
0: 1 1 1 1
1: 0 0 0 0
1: 0 0 0 0
0: 1 1 1 1
1: 0 0 0 0
0: 1 1 1 1
0: 1 1 1 1
0: 1 1 1 1
1: 0 0 0 0
1: 0 0 0 0
1: 0 0 0 0
1: 0 0 0 0
0: 1 1 1 1
0: 1 1 1 1
sizeof(BIT) = 4; sizeof(word) = 64

可编译测试用例 - 源

#include "logic.h"

void word_not_1(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
    {
        if ((*A)[i])
            (*R)[i] = ZERO;
        else
            (*R)[i] = ONE;
    }
}

void word_not_2(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
        (*R)[i] = !(*A)[i];
}

void word_not_3(word R, word A)
{
    for (int i = 0; i < 16; i++)
    {
        if (A[i])
            R[i] = ZERO;
        else
            R[i] = ONE;
    }
}

void word_not_4(word R, word A)
{
    for (int i = 0; i < 16; i++)
        R[i] = !A[i];
}

#include <stdio.h>

int main(void)
{
    word in =
    {
        ZERO,  ONE, ZERO, ONE,
        ONE,  ZERO, ONE,  ZERO,
        ZERO, ZERO, ONE,  ONE,
        ONE,  ONE,  ZERO, ZERO,
    };
    word out1;
    word out2;
    word out3;
    word out4;

    word_not_1(&out1, &in);
    word_not_2(&out2, &in);
    word_not_3(out3, in);
    word_not_4(out4, in);

    for (int i = 0; i < 16; i++)
        printf("%d: %d %d %d %d\n", in[i], out1[i], out2[i], out3[i], out3[i]);

    printf("sizeof(BIT) = %zu; sizeof(word) = %zu\n", sizeof(BIT), sizeof(word));
    return 0;
}

Context

One version of the question included the code:

Source file logic.c

#include "logic.h"
void word_not(word *R, word *A)
{
    for (int i = 0; i<16; i++)
    {
        if ((A+i))
            {*(R+i) = ZERO;}
        else
            {*(R+i) = ONE; }
    }
}

header file logic.h

#ifndef LOGIC_H_
#define LOGIC_H_

#include <stdint.h>

/**
 * Basic element of the LC-3 machine.
 *
 * BIT takes on values ZER0 (0) or ONE (1)
 */
typedef enum {
ZERO = (uint8_t) 0, ONE = (uint8_t) 1
} BIT;

/**
* A 16-"BIT" LC-3 word in big-endian format
*/
typedef BIT word[16];

void word_not(word *R, word *A);

#endif

Explanation of the problem

The problem is that arguments to word_not() are pointers to arrays. The notation inside has to be adjusted accordingly:

void word_not(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
    {
        if ((*A)[i])
            (*R)[i] = ZERO;
        else
            (*R)[i] = ONE;
    }
}

Though you could write that more succinctly as:

void word_not(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
        (*R)[i] = !(*A)[i];
}

Alternatively, you can simply redefine the function as:

void word_not(word R, word A)
{
    for (int i = 0; i < 16; i++)
    {
        if (A[i])
            R[i] = ZERO;
        else
            R[i] = ONE;
    }
}

Or, again, more succinctly, as:

void word_not(word R, word A)
{
    for (int i = 0; i < 16; i++)
        R[i] = !A[i];
}

Compilable test case - output

0: 1 1 1 1
1: 0 0 0 0
0: 1 1 1 1
1: 0 0 0 0
1: 0 0 0 0
0: 1 1 1 1
1: 0 0 0 0
0: 1 1 1 1
0: 1 1 1 1
0: 1 1 1 1
1: 0 0 0 0
1: 0 0 0 0
1: 0 0 0 0
1: 0 0 0 0
0: 1 1 1 1
0: 1 1 1 1
sizeof(BIT) = 4; sizeof(word) = 64

Compilable test case - source

#include "logic.h"

void word_not_1(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
    {
        if ((*A)[i])
            (*R)[i] = ZERO;
        else
            (*R)[i] = ONE;
    }
}

void word_not_2(word *R, word *A)
{
    for (int i = 0; i < 16; i++)
        (*R)[i] = !(*A)[i];
}

void word_not_3(word R, word A)
{
    for (int i = 0; i < 16; i++)
    {
        if (A[i])
            R[i] = ZERO;
        else
            R[i] = ONE;
    }
}

void word_not_4(word R, word A)
{
    for (int i = 0; i < 16; i++)
        R[i] = !A[i];
}

#include <stdio.h>

int main(void)
{
    word in =
    {
        ZERO,  ONE, ZERO, ONE,
        ONE,  ZERO, ONE,  ZERO,
        ZERO, ZERO, ONE,  ONE,
        ONE,  ONE,  ZERO, ZERO,
    };
    word out1;
    word out2;
    word out3;
    word out4;

    word_not_1(&out1, &in);
    word_not_2(&out2, &in);
    word_not_3(out3, in);
    word_not_4(out4, in);

    for (int i = 0; i < 16; i++)
        printf("%d: %d %d %d %d\n", in[i], out1[i], out2[i], out3[i], out3[i]);

    printf("sizeof(BIT) = %zu; sizeof(word) = %zu\n", sizeof(BIT), sizeof(word));
    return 0;
}
心凉怎暖 2024-12-14 11:24:26

我认为问题在于您试图在函数范围之外的文件范围内执行 Bob[10] = ten; 赋值。你不能那样做。在文件范围内,您无法自由索引内容,也无法使用常量值以外的任何内容初始化变量,十不是其中之一。

现在,我有点模糊为什么下面的代码不想编译(使用 gcc 3.4.4 和 Open Watcom 1.9):

typedef enum {
ZERO = 0, ONE = 1
} BIT;

typedef BIT word[16];

const BIT ten = ZERO;

word Bob =
{
  ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  ZERO, ZERO, ten, ZERO, ZERO, ZERO, ZERO, ZERO
};

int Bob2[] =
{
  ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  ZERO, ZERO, ten, ZERO, ZERO, ZERO, ZERO, ZERO
};

int main(int argc, char** argv)
{
  return 0;
}

两个编译器都说 Bob[10] 和 Bob2[10] 没有用常量初始化。

I think the problem is that you're trying to do the Bob[10] = ten; assignment outside of function scope, at the file scope. You can't do that. At the file scope you can't index things freely and you can't initialize variables with anything other than constant values, ten isn't one of them.

Now, I'm a little fuzzy as to why the below doesn't want to compile (using gcc 3.4.4 and Open Watcom 1.9):

typedef enum {
ZERO = 0, ONE = 1
} BIT;

typedef BIT word[16];

const BIT ten = ZERO;

word Bob =
{
  ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  ZERO, ZERO, ten, ZERO, ZERO, ZERO, ZERO, ZERO
};

int Bob2[] =
{
  ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO,
  ZERO, ZERO, ten, ZERO, ZERO, ZERO, ZERO, ZERO
};

int main(int argc, char** argv)
{
  return 0;
}

Both compilers say Bob[10] and Bob2[10] aren't being initialized with constants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文