c语言数组可以存储不同类型数据

#include "StdAfx.h"
#include <stdio.h>
#include <stdlib.h>

typedef union {
    int         intType;
    char        charType;
    float       floatType;
    const char* stringType;
} AnyType;

int main() {
    AnyType anyType[4];
    anyType[0].intType    = 1;
    anyType[1].charType   = '2';
    anyType[2].floatType  = 3.4f;
    anyType[3].stringType = "567";
    printf("%d\n", anyType[0].intType);
    printf("%c\n", anyType[1].charType);
    printf("%f\n", anyType[2].floatType);
    printf("%s\n", anyType[3].stringType);
    system("pause");
    return 0;
}

 

posted @ 2020-07-07 17:25  温暖了寂寞  阅读(1855)  评论(0编辑  收藏  举报