struct 的定义和使用
点击(此处)折叠或打开
-
/*************************************************************************
-
> File Name: 5-3.c
-
> Author: sheepbao
-
> Mail: 13116160592@163.com
-
> Created Time: 2015年07月23日 星期四 15时34分01秒
-
************************************************************************/
-
-
#include<stdio.h>
-
#include<string.h>
-
int main()
-
{
-
struct student //define struct name
-
{ // struct member
-
int number ;
-
char name[20];
-
char sex;
-
int age;
-
char addr[30];
-
-
};
-
//define struct var
-
struct student s[3]={ { 10000,"zhangsan",'m',10,"china"},{10001,"lisi",'w',3,"us"} };
-
s[2].number=1002;
-
s[2].sex='m';
-
s[2].age=23;
-
strcpy(s[2].name,"haha");
-
strcpy(s[2].addr,"xianshi");
-
int i;
-
for (i=0;i<3;i++)
-
{
-
printf("%d,%s,%c,%d,%s\n",s[i].number,s[i].name,s[i].sex,s[i].age,s[i].addr);
-
-
}
-
struct student *p;
-
-
for (p=s;p<s+3;p++)
-
printf("%d,%s,%c,%d,%s\n",p->number,p->name,p->sex,p->age,p->addr);
-
return 0;
-
-
-
}
结构体的长度用sizeof求,其长度和边界对齐有关系
-
#include<stdio.h>
-
-
int main()
-
{
-
struct data
-
{
-
long l;
-
char *s;
-
short int i;
-
char c;
-
short int a[5];
-
}d;
-
-
struct data *p = &d;
-
-
printf("%d\n",sizeof(d));
-
printf("%x\t%x\n",p,p+1);
-
printf("%x\t%x\n",p,(char *)p+1);
-
printf("%x\t%x\n",p,(long *)p+1);
-
-
return 0;
-
}
阅读(1084) | 评论(0) | 转发(0) |