C語言62,63課代碼

結構體和聯合用法我認爲很類似:ide

#include <stdio.h>
struct world
{
    union like{
        int ss[10];
        int ss1[10];        
    }ab;
}ac = {1,2,3,4,5,6,7,8,9,0};
union hello
{
    int age;
    float height;
    char k[5];
};
int main(void)
{
    union hello he;
    he.age = 10;
    printf("%d\n",he.age);   //和下面一句打印的效果是同樣的
    printf("%d\n",he.k[0]);
    
    printf("%d\n",ac.ab.ss[3]);
    return 0;
}

//在聯合或者結構體中,訪問某個元素須要用到'.'或者是->,而在枚舉中直接寫出元素就能夠:
#include <stdio.h>
enum color{red = 100,yellow = 200,blue = 400};
int main(void)
{
    printf("%d\n",red);  //0
    printf("%d\n",red);   //100

    return 0;    
}

指向函數的指針,他嗎的指針誰都敢指,呵呵:

#include <stdio.h>
void HELLO(char);
void hello(char);
void (*fp)(char);
int main(void)
{
    char c;
    c = getchar();
    if(c >= 'a' && c <= 'z')
    {
        fp = HELLO;
    }else if(c >= 'A' && c <= 'Z'){
        fp = hello;
    }
    fp(c);
    putchar('\n');
    return 0;
}

void HELLO(char ch)
{
    putchar(ch - 'a' + 'A');
}

void hello(char ch)
{
    putchar(ch + 'a' - 'A');
}
相關文章
相關標籤/搜索