分类: 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);
}
}
运行效果: