zoukankan      html  css  js  c++  java
  • struct and union example

    1. StructHandler.c:

    /*
     * StructHandler.c
     *
     *  Created on: Jul 6, 2013
     *      Author: wangle
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void modifyName(struct student *p);



    int main(){
        struct student{
            char name[50];
            char dep[50];
            long no;
            float score[4];
        };

        typedef struct student stu_t;

        struct student stu[50]={
            "wangle", "Math", 80,80,90.5,99,100,
            "xuyehui", "biological", 90,90,90,70,100,
            "mengmeng", "Math", 100,100,100,100,90
        };
        int i;
        for(i=0; i<3; i++){
            printf("%s, %s, %ld, %.2f,%.2f,%.2f,%.2f ", stu[i].name, stu[i].dep, stu[i].no,
                    stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3]);
        }

        stu_t * p = stu;
        puts((*(p+1)).name);     //(1) a pointer call style.
        puts((p+1)->name);       //(2) common pointer call style. (1) and (2) is the same.

        printf("%s ", (p+2)->name);
        printf("%s ", (*(p+2)).name);
        printf("no = %ld ", p->no);
        void modifyName(struct student * p){
                p->no = 123456;
        }

        modifyName(p);
        printf("%s ", (p)->name);
        printf("no = %ld ", p->no);
    }

    2. UnionHandler.c

    /*
     * UnionHandler.c
     *
     *  Created on: Jul 6, 2013
     *      Author: wangle
     */

    #include <stdio.h>
    int main(){
        union unidate{
            char c;
            int i;
            long l;
            float f
        };
        union unidate x;
        x.c=65;
        printf("c=%c ", x.c);
        x.i = 10;
        printf("i=%d ",x.i);
        x.l = 100;
        printf("l=%ld ", x.l);
        x.f = 90.5;
        printf("f=%.1f ", x.f);
        printf("c=%c ", x.c);
    }

  • 相关阅读:
    JQ和Js获取span标签的内容
    JS获取子节点、父节点和兄弟节点的方法实例总结
    JS实现系统时间(自动)
    CSS font-family 属性
    网页中导入特殊字体@font-face属性详解
    ****HTML模板资源汇总
    ***XAMPP:报错 Unable to load dynamic library的解决方法
    2016年宜昌楼市将迎来史上最激烈一战
    北大资源重磅来宜--宜昌未来商业中心将诞生
    HTML5调用传感器的资料汇总
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/3175941.html
Copyright © 2011-2022 走看看