开发者

Incompatible enum types in C

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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜