Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293885
  • 博文数量: 70
  • 博客积分: 1990
  • 博客等级: 上尉
  • 技术积分: 686
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-02 08:52
文章分类

全部博文(70)

文章存档

2015年(2)

2014年(9)

2013年(2)

2012年(20)

2011年(1)

2010年(36)

分类: C/C++

2012-03-31 22:17:51

结构体的赋值,有两种方法:使用元素引用赋值,使用指针赋值。

源码:

#include

 

struct test

{

       int a;

       char * str;

};

 

int main()

{

       struct test m_test;

      

       printf("--------------1------------\n");

       printf("struct value by reference\n");

 

       m_test.a = 123;

       m_test.str = "string";    

 

       printf("a = %d\n str:%s\n",m_test.a,m_test.str);

 

       {

              struct test n_test;

              struct test *pst;

             

              printf("\n--------------2-------------\n");

              printf("struct value by pointer\n");

              pst = &n_test;

              pst->a = 234;

              pst->str = "hello";

             

              printf("a = %d\n str:%s\n",pst->a,pst->str);

             

             

       }

      

}

 

运行效果:

 

 

阅读(995) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~