首页 文章

如何将char int存储为结构中的相同类型和相同成员?

提问于
浏览
-2

我希望在一个结构中有一个类型,它接受一个char和一个int,即Z84 . 我试图解决这个问题,但我没有任何线索 . 会无效吗?或者它与不同类型的冲突是否相同?

struct exmaple{

whatType? charValueintValue

};

我在巫婆中搜索类似int的类型,我可以存储Z84 . I do not mean this:

struct exmaple{

int number
char value 
};

但是如果我可以做printf(“某些东西:%示例”,示例),或者我应该怎么做才能在charValueintValue上使用printf用printf打印出这个值,这样可以正常工作?

2 回答

  • 2

    您应该将其存储为char *并将整数值转换为ASCII .

    struct example{
        char value[LENGHT];
    };
    

    如果要使用整数,还可以尝试单独存储它们:

    struct example{
        char charValue;
        int intValue;
    };
    
  • 2

    当然,这很容易:

    struct example {
        char_and_int x;
    };
    

    现在你有“结构中的单一类型” .

    您只需要更早地定义:

    struct char_and_int {
        char c;
        int  i;
    };
    

    用法:

    struct example foo = { { 'Z', 89 } };
    

相关问题