不兼容的枚举类型用C [英] Incompatible enum types in C

查看:98
本文介绍了不兼容的枚举类型用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于枚举和数组的问题。基本上我有声明为枚举类型字。

枚举BITS数组

 的typedef枚举{
ZERO =(uint8_t有)0,ONE =(uint8_t有)1
} BIT;的typedef位字[16];

由于这是向我解释,字仅仅是16位的predefined阵列。然而,当我试图给一个声明的话,我只是得到一个错误说不相容的类型字和位。

  BIT了TEN =零;
字鲍勃;
鲍勃·[10] =十天,

我可以只写与另一个词的词鲍勃,我会因为字认为是位S数组,我可以有点简单地分配给字数组中的位置。


解决方案

上下文

问题的一个版本包括了code:


  

源文件logic.c

 <$ C C>的#includelogic.h
无效word_not(字* R,字* A)
{
    的for(int i = 0; I&LT; 16;我++)
    {
        如果((A + I))
            {*(R + I)=零;}
        其他
            {*(R + I)= 1; }
    }
}


  
  

头文件logic.h

 的#ifndef LOGIC_H_
#定义LOGIC_H_#包括LT&;&stdint.h GT;/ **
 *在LC-3机的基本元素。
 *
 * BIT呈现值ZER0(0)或一(1)
 * /
的typedef枚举{
ZERO =(uint8_t有)0,ONE =(uint8_t有)1
} BIT;/ **
*一个16BITLC-3 big-endian格式字
* /
的typedef位字[16];无效word_not(字* R,字* A);#万一



问题的说明

问题是,参数 word_not()是指向数组的指针。符号内部具有作相应的调整:

 无效word_not(字* R,WORD * A)
{
    的for(int i = 0; I&LT; 16;我++)
    {
        如果((* A)[我])
            (* R)[I] =零;
        其他
            (* R)[I] =之一;
    }
}

虽然你可以编写更简洁的:

 无效word_not(字* R,字* A)
{
    的for(int i = 0; I&LT; 16;我++)
        (* R)[I] =(* A)[I]!;
}

另外,你可以简单地重新定义函数:

 无效word_not(字R,A字)
{
    的for(int i = 0; I&LT; 16;我++)
    {
        如果(A [I])
             - [R [i] =零;
        其他
             - [R [i] =之一;
    }
}

或者再次,更简洁,如:

 无效word_not(字R,A字)
{
    的for(int i = 0,I&LT; 16;我++)
         - [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(字)= 64

可编译测试用例 - 源

 的#includelogic.h无效word_not_1(字* R,字* A)
{
    的for(int i = 0; I&LT; 16;我++)
    {
        如果((* A)[我])
            (* R)[I] =零;
        其他
            (* R)[i] = ONE,
    }
}无效word_not_2(字* R,字* A)
{
    的for(int i = 0; I&LT; 16;我++)
        (* R)[I] =(* A)[I]!;
}无效word_not_3(字R,A字)
{
    的for(int i = 0; I&LT; 16;我++)
    {
        如果(A [I])
             - [R [i] =零;
        其他
             - [R [i] =之一;
    }
}无效word_not_4(字R,A字)
{
    的for(int i = 0; I&LT; 16;我++)
         - [R [i] = A [I]!;
}#包括LT&;&stdio.h中GT;INT主要(无效)
{
    字=
    {
        零,一零,一,
        一,零,一,零,
        零,零,一,一,
        其中,一,零,零,
    };
    字OUT1;
    字OUT2;
    字OUT3;
    字OUT4;    word_not_1(安培; OUT1,&安培;中);
    word_not_2(安培; OUT2,和放大器;中);
    word_not_3(OUT3中);
    word_not_4(OUT4,在);    的for(int i = 0; I&LT; 16;我++)
        的printf(%d个:%D%D \\ n,在[I],OUT1 [I],OUT2 [I],OUT3 [I],OUT3 [I]);    的printf(的sizeof(BIT)=%祖;的sizeof(字)=%祖\\ N的sizeof(BIT)的sizeof(字));
    返回0;
}

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

这篇关于不兼容的枚举类型用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆