指向结构体指针的例子

#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=数组名

 

 

你可能感兴趣的:(结构体)