Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2371739
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: C/C++

2011-04-29 09:20:49

c语言之结构体

1.       结构体基础知识

C语言提供了两种类型的聚合数据类型(能够同时存储超过一个的单独数据),数组和结构。数则是相同类型的元素的集合,而结构也是一些值的集合,这些值称为它的成员,单一个结构的各个成员可能具有不同的类型。

由于结构成员不一定是同种数据类型,所以不能用类似于数组的引用方式,而是用成员名来引用。

 

1.1 结构声明:

struct tag {member-list} variable-list;//注意这里的分号

sruct item

{

         int a;

         char b;

         float c;

}v1,v2={1,'2',3.0};

         这里声明了两个变量v1v2v2的成员并初始化了。

 

1.2 结构引用:

sruct item *p;                                          

(1)        指针变量:p->a;   p-b;   (*p).va

(2)        普通变量:v1.a;   v2.b;   (&v1)->a

(3)        结构的自引用:

非法:sruct item p; 注意这里声明的是一个结构变量,如果这样声明会类似于递归无休止的下去,非法的。

sruct item

{      

     int a;

     char b;

     float c;

     sruct item p;

};

合法: sruct item *p;注意这里分配的是一个指针,指针是固定大小的,需要用的时候分配空间。

sruct item

{      

     int a;

     char b;

     float c;

     sruct item *p;

};

 

1.3    结构的存储分配

系统并不像结构里面所定义的变量分配相应的空间,结构所占的空间大小还要算上由于边界对齐(数据对齐)所带来的额外内存空间,为什么会出现数据对齐呢?

         8位的CPU当然不会产生数据对齐,但是发展到16位,32位时就会产生,以32位的为例,CPU一次能够内存访问的是4个字节的数据量,那么如果一个1字节的数据存储在里面当然就没有问题,但是如果2字节,或者4字节的数据存储时候超过了边界,跨越了两个4的倍数的单位,那么就要读取两次了。

例如,下面的结构各成员空间分配情况:
struct test
{
     char x1;
     short x2;
     float x3;
     char x4;
};

结构的第一个成员x1,其偏移地址为0,占据了第1个字节。第二个成员x2short类型,其起始地址必须2字节对界,因此,编译器在x2x1之间填充了一个空字节。结构的第三个成员x3和第四个成员x4恰好落在其自然对界地址上,在它们前面不需要额外的填充字节。在test结构中,成员x3要求4字节对界,是该结构所有成员中要求的最大对界单元,因而test结构的自然对界条件为4字节,编译器在成员x4后面填充了3个空字节。整个结构所占据空间为12字节。

如果我们改变其结构内部的顺序则可以改变其整体大小,

struct test

{

     char x1;

     char x4;

     short x2;

     float x3;

};

Sizeof操作符能够得出一个结构的整体大小,包括因边界对齐而跳过的部分,宏offsetof(定义于stddef.h)可以得到每个成员的具体位置,

Offsetof(type, member)

例如:offsetof(struct test, x1) struct test为结构体,x1为结构里面的成员名

测试程序:

#include

#include

 

struct test1

{

     char x1;

     short x2;

     float x3;

     char x4;

};

struct test2

{

     char x1;

     char x4;

     short x2;

     float x3;

};

int main(void)

{

printf("test 1 size is %d\n", sizeof(struct test1));

printf("x1 offset is %d\n", offsetof(struct test1, x1));

printf("x2 offset is %d\n", offsetof(struct test1, x2));

printf("x3 offset is %d\n", offsetof(struct test1, x3));

printf("x4 offset is %d\n", offsetof(struct test1, x4));

printf("test 2 size is %d\n", sizeof(struct test2));

printf("x1 offset is %d\n", offsetof(struct test2, x1));

printf("x4 offset is %d\n", offsetof(struct test2, x4));

printf("x2 offset is %d\n", offsetof(struct test2, x2));

printf("x3 offset is %d\n", offsetof(struct test2, x3));

return 0;

}

程序结果:

test 1 size is 12

x1 offset is 0

x2 offset is 2

x3 offset is 4

x4 offset is 8

test 2 size is 8

x1 offset is 0

x4 offset is 1

x2 offset is 2

x3 offset is 4

请按任意键继续. . .

 

边界对齐的规则:

第一,编译器按照成员列表的顺序给每个成员分配内存.
  第二,当成员需要满足正确的边界对齐时,成员之间用额外字节填充.
  第三,结构体的首地址必须满足结构体中边界要求最为严格的数据类型所要求的地    .
  第四,结构体的大小为其最宽基本类型的整数倍.

 

#pragma pack(n)指令

在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配空间。一般地,可以通过下面的方法来改变缺省的对界条件:
     ·
使用伪指令#pragma pack (n)C编译器将按照n个字节对齐。
     ·
使用伪指令#pragma pack (),取消自定义字节对齐方式。

 

1.4    结构作为函数参数:由于结构作为函数参数,一般结构大小较大,直接作为函数参数效率较低,会大大降低栈的大小(函数参数放于栈上),我们一把采用的是指针传递,这样就需要注意指针使用的安全性,必要的时间尽量加上限定符const

例如:

void print(struct test1 mytest)

{

         printf("x1 = %c, x3 = %f\n",  mytest.x1, mytest.x3);

}

void print(struct test1 *mytest)

{

         printf("x1 = %c, x3 = %f\n",  mytest->x1, mytest->x3);

}

 

由于函数内部不改变其值,所有我们应该限定为指针指向的为常量

void print(struct test1 * const mytest)

{

         printf("x1 = %c, x3 = %f\n",  mytest->x1, mytest->x3);

}

 

2.1       typedef与结构的联合应用

 

typedef具体用法见:http://blog.chinaunix.net/space.php?uid=25324849&do=blog&id=229496

typedefdefine的区别:

http://blog.chinaunix.net/space.php?uid=25324849&do=blog&id=229496

 

我们经常看到这样的用法:

typedef struct node

{

          char x1;

     char x4;

     short x2;

     float x3;

}NODE, *PNODE;

我们可以拆开了慢慢讲,上面的定义顺序完成的是

1)首先是结构体得定义

struct node

{

          char x1;

     char x4;

     short x2;

     float x3;

};

结构名为node,我们可以利用结构名定义结构变量;然后就是下一步进行的是

2typedef struct node NODE;// struct node创建一个名字为NODE

typedef struct node * PNODE;// struct node*创建一个名字为PNODE

需要注意的是,在C99标准里面,不将struct node写完全是不能通过编译的(现在可以写成node,注意在C语言里面这个好像也不能通过编译的,估计就是该编译器使用的是C99标准,但是在C++里面可以通过编译),我们应该养成好习惯.

如果我们在声明结构体的时候忽略结构名得话,必须在声明的时候定义变量不然后面将无法使用该结构了,但是你也可以使用,比如:

typedef struct

{

          char x1;

     char x4;

     short x2;

     float x3;

}mNODE, *pnode;

但是如果你没有上面定义别名,那么你只能按下面的方法声明并定义变量

struct

{

          char x1;

     char x4;

     short x2;

     float x3;

    

}node1;

 

3)由于定义顺序是按照(1)(2),进行的,所有下面的代码是通不过编译的

typedef struct node

{

          char x1;

     char x4;

     short x2;

     float x3;

     PNODE l;

}NODE, *PNODE;

4)需要注意的是结构体指针,我们在定义结构指针的时候一定要初始化为NULL,使用的使用必须给他动态分配空间,除非已经有结构体变量了,你可以指针指向它,这就不需要分配动态空间。

         5)假如已经有了结构体struct node,我们定义

typedef  struct node  NODE

typedef  NODE * PNODE 等同于typedef struct node * PNODE

其实我们为了更具有阅读性,我们应该选择更易理解的定义方式,而不是写出很具有艺术性的代码,让大家都不是很容易懂。

         6)自定义结构数组:如果我们自定义结构数组很容易像下面这样使用:

         typedef struct node  [10] ANODE;

         正确的用法是:

         typedef struct node  ANODE [10];

或者

typedef struct node

{

          char x1;

     char x4;

     short x2;

     float x3;

}NODE, *PNODE, ANODE [10];

         定义变量结构数组:

ANODE array1;//代表的是struct node array1[10]

ANODE array2[2];// 代表的是struct node array2[2][10]

测试用例:

#include

#include

 

 

typedef struct node

{

          char x1;

     char x4;

     short x2;

     float x3;

}NODE, *PNODE, ANODE [10];

 

ANODE array1;

ANODE array2[2];

 

int main(void)

{

         int a[2][10];

         printf("struct node size is %d\n", sizeof(struct node));

         printf("array1 size is %d\n", sizeof(array1));

         printf("array2 size is %d\n", sizeof(array2));

         printf("array2[0] size is %d\n", sizeof(array2[0]));

         printf("array2[0][3] size is %d\n", sizeof(array2[0][3]));

         printf("array2[2][3] size is %d\n", sizeof(array2[2][3]));

 

         printf("a[0][0] offset is %d\n", &a[0][0]);

         printf("a[0][9] offset is %d\n", &a[0][9]);

         printf("a[1][0] offset is %d\n", &a[1][0]);

         printf("a[1][9] offset is %d\n", &a[1][9]);

        

         printf("a[0] offset is %d\n", &a[0]);

         printf("a[1] offset is %d\n", &a[1]);

        

         printf("array1[1] offset is %d\n", &array1[1]);

         printf("array1[0] offset is %d\n", &array1[0]);

         printf("array2[1] offset is %d\n", &array2[1]);

         printf("array2[0] offset is %d\n", &array2[0]);

        

         return 0;

}

程序结果:

struct node size is 8

array1 size is 80

array2 size is 160

array2[0] size is 80

array2[0][3] size is 8

array2[2][3] size is 8

a[0][0] offset is 37814032

a[0][9] offset is 37814068

a[1][0] offset is 37814072

a[1][9] offset is 37814108

a[0] offset is 37814032

a[1] offset is 37814072

array1[1] offset is 4206760

array1[0] offset is 4206752

array2[1] offset is 4206672

array2[0] offset is 4206592

请按任意键继续. . .

 

结果分析:我们可以看出&a[1]-&a[0]10个整型的大小,因为我们定义的是 int a[2][10]

现在我可以用类似的方法测试ANODE array2[2];到底是array2[2][10],还是array2[10][2]

由结果

array2[1] offset is 4206672

array2[0] offset is 4206592

我们可以知道,ANODE array2[2]定义的是array2[2][10],这是二维的数组,多维数组可以用类似的方法测试。

 

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