zoukankan      html  css  js  c++  java
  • 指向结构体指针的例子

    #include<stdio.h>
    #define LEN 20
    struct names
    {
      
    char first[LEN];
      
    char last[LEN];
    };

    struct guy
    {
      
    struct names handle;
      
    char favfood[LEN];
      
    char job[LEN];
      
    float income;
    };

    int main(void)
    {
         
    struct guy fellow[2]=
         {
            {{"Ewen","Villard"},"grilled salmon","personality coach",58112.00},
            {{"Rodney","Swillbelly"},"tripe""tabbid editor",232400.00}
        };
         
        
    struct guy *him;

        printf("address #1: %p  #2: %p\n",&fellow[0],&fellow[1]);
        
        him=&fellow[0];

        printf("pointer #1: %p #2: %p\n",him,him+1);

        printf("him->income is $%.2f  (*him)income is $%.2f\n",him->income,(*him).income);

        him++;

        printf("him->favfood is %s  him->handle.last is %s\n",him->favfood,him->handle.last);

        
    return 0;
    }
    输出结果

    address #1:0x0012fea4 #2:0x0012fef8

    pointer #1:0x0012fea4 #2:0x0012fef8

    him->income is $58112.00: (*him).income is $58112.00

    him->favfood is tripe: him->handle.last is Swillbelly
     

     

    结构(fellow)作为函数的参数传递分类 
    1:传递结构成员      例如:function(fellow[0].income)
    2:传递结构地址      例如:function(&fellow[0])
    3:把结构作为参数传递  例如:function(fellow[0])
    4:把一个结构赋值给另外一个结构 例如:fellow[0]=fellow[1]
    5:结构不能直接赋值给指针(需&取地址),而数组可以直接赋值给指针   例如:p=&fellow[0] 或 p=数组名

  • 相关阅读:
    java动态注册Filter,Servlet,Listener
    java防止html脚本注入
    java中常用的几种跨域方式
    backbone 要点知识整理
    创建对象-constructor丢失的问题
    css3 动画(animation)-简单入门
    sass安装
    sass安装步骤
    javascript how sort() work
    javascript 获取调用属性的对象
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/2058196.html
Copyright © 2011-2022 走看看