还是结构体的一些问题(转)

来源:互联网 发布:php 高内聚 低耦合 编辑:程序博客网 时间:2024/06/05 09:47

函数指针作为结构体成员

结构体指针变量的定义,定义结构体变量的一般形式如下:

形式1:先定义结构体类型,再定义变量
struct结构体标识符
{
成员变量列表;…
};
struct 结构体标识符 *指针变量名;

变量初始化一:struct结构体标识符 变量名={初始化值1,初始化值2,…, 初始化值n };


形式2:在定义类型的同时定义变量
struct结构体标识符
{
成员变量列表;…
} *指针变量名;

变量初始化二:


形式3:直接定义变量,用无名结构体直接定义变量只能一次
struct
 {
成员变量列表;…
}*指针变量名;


其中“指针变量名”为结构体指针变量的名称。形式1是先定义结构体,然后再定义此类型的结构体指针变量;形式2和形式3是在定义结构体的同时定义此类型的结构体指针变量。


函数指针的定义

  一般的函数指针可以这么定义:

  int(*func)(int,int);

  表示一个指向含有两个int参数并且返回值是int形式的任何一个函数指针. 假如存在这样的一个函数:

  int add2(int x,int y)

  {

  return x+y;

  }

  那么在实际使用指针func时可以这样实现:

  func=&add2; //指针赋值,或者func=add2; add2与&add2意义相同

  printf("func(3,4)=%d\n",func(3,4));

  事实上,为了代码的移植考虑,一般使用typedef定义函数指针类型.

  typedef int(*FUN)(int,int);

  FUN func=&add2;

  func();



结构体中包含函数指针

其实在结构体中,也可以像一般变量一样,包含函数指针变量.下面是一种简单的实现.

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. struct DEMO  
  3. {  
  4. int x,y;  
  5. int (*func)(int,int); //函数指针  
  6. };  
  7.   
  8. int add1(int x,int y)  
  9. {  
  10. return x*y;  
  11. }  
  12.   
  13. int add2(int x,int y)  
  14. {  
  15. return x+y;  
  16. }  
  17.   
  18. void main()  
  19. {  
  20. struct DEMO demo;  
  21. demo.func=add2; //结构体函数指针赋值  
  22. //demo.func=&add2; //结构体函数指针赋值  
  23. printf("func(3,4)=%d\n",demo.func(3,4));  
  24. demo.func=add1;  
  25. printf("func(3,4)=%d\n",demo.func(3,4));  
  26. }  
  27.   
  28. /* 
  29. 输出: 
  30. func(3,4)=7 
  31. func(3,4)=12 
  32. */  


结构体中指向函数的指针                                          

C语言中的struct是最接近类的概念,但是在C语言的struct中只有成员,不能有函数,但是可以有指向函数的指针,这也就方便了我们使用函数了。举个例子,如下:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. typedef struct student  
  6. {  
  7.     int id;  
  8.     char name[50];   
  9.     void (*initial)();  
  10.     void (*process)(int id, char *name);  
  11.     void (*destroy)();  
  12. }stu;  
  13.   
  14. void initial()  
  15. {  
  16.     printf("initialization...\n");  
  17. }  
  18.   
  19. void process(int id, char *name)  
  20. {  
  21.     printf("process...\n%d\t%s\n",id, name);  
  22. }  
  23.   
  24. void destroy()  
  25. {  
  26.     printf("destroy...\n");  
  27. }  
  28.   
  29. int main()  
  30. {  
  31.     stu *stu1;  
  32.     //在VC和TC下都需要malloc也可以正常运行,但是linux gcc下就会出错,为段错误,必须malloc  
  33.     stu1=(stu *)malloc(sizeof(stu));  
  34.     //使用的时候必须要先初始化  
  35.     stu1->id=1000;  
  36.     strcpy(stu1->name,"C++");  
  37.     stu1->initial=initial;  
  38.     stu1->process=process;  
  39.     stu1->destroy=destroy;  
  40.     printf("%d\t%s\n",stu1->id,stu1->name);  
  41.     stu1->initial();  
  42.     stu1->process(stu1->id, stu1->name);  
  43.     stu1->destroy();  
  44.     free(stu1);  
  45.     return 0;  
  46. }  

输出:

/*
1000    C++
initialization...
process...
1000    C++
destroy...
*/

c语言中,如何在结构体中实现函数的功能?把结构体做成和类相似,让他的内部有属性,也有方法
这样的结构体一般称为协议类,提供参考: 
struct { 
 int funcid; 
 char *funcname; 
 int (*funcint)(); /* 函数指针 int 类型*/ 
 void (*funcvoid)(); /* 函数指针 void类型*/ 
}; 
每次都需要初始化,比较麻烦

 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. typedef struct  
  4. {  
  5. int a;  
  6. void (*pshow)(int);  
  7. }TMP;  
  8.   
  9. void func(TMP *tmp)  
  10. {  
  11.     if(tmp->a >10)//如果a>10,则执行回调函数。  
  12.     {  
  13.         (tmp->pshow)(tmp->a);  
  14.     }  
  15. }  
  16.   
  17. void show(int a)  
  18. {  
  19.     printf("a的值是%d\n",a);  
  20. }  
  21.   
  22. void main()  
  23. {  
  24.     TMP test;  
  25.     test.a = 11;  
  26.     test.pshow = show;  
  27.     func(&test);  
  28. }  
  29.   
  30. /* 
  31. 一般回调函数的用法为: 
  32. 甲方进行结构体的定义(成员中包括回调函数的指针) 
  33.  
  34. 乙方定义结构体变量,并向甲方注册, 
  35. 甲方收集N个乙方的注册形成结构体链表,在某个特定时刻遍历链表,进行回调。 
  36. 当 函数指针 做为函数的参数,传递给一个被调用函数, 
  37. 被调用函数就可以通过这个指针调用外部的函数,这就形成了回调<p>一般的程序中回调函数作用不是非常明显,可以不使用这种形式</p><p>最主要的用途就是当函数不处在同一个文件当中,比如动态库,要调用其他程序中的函数就只有采用回调的形式 
  38. 通过函数指针参数将外部函数地址传入来实现调用</p><p>函数的代码作了修改,也不必改动库的代码,就可以正常实现调用便于程序的维护和升级</p>*/  



1 C语言数组灵活多变的访问形式

我们知道,C语言中的数组大小是固定的,定义的时候必须要给一个常量值,不能是变量。

这带来了很大的不便,如果数组过小,不能容下所有数组,如果过大,浪费资源。

请实现一个简单的动态数组,能够随时改变大小,不会溢出,也不会浪费内存空间。

下面的代码实现了简单的动态数组:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. //从控制台获取初始数组大小
  6. int N;
  7. int *a;
  8. int i;
  9. printf("Input array length:");
  10. scanf("%d\n",&N);
  11. //分配空间
  12. a=(int *)calloc(N,sizeof(int));
  13. //填充数据
  14. for(i=0;i<N;i++){
  15. a[i]=i+1;
  16. printf("%-5d",a[i]);
  17. if((i+1)%10==0){
  18. printf("\n");
  19. }
  20. }
  21. //释放内存
  22. free(a);
  23. a=NULL;
  24. printf("\n");
  25. return 0;
  26. }
运行结果:
Input array length:201    2    3    4    5    6    7    8    9    1011   12   13   14   15   16   17   18   19   20
2 C语言内存分配

realloc()函数

原型:extern void *realloc(void *mem_address, unsigned int newsize);

语法:指针名=(数据类型*)realloc(要改变内存大小的指针名,新的大小)。

头文件:#include <stdlib.h> 有些编译器需要#include <alloc.h>,在TC2.0中可以使用alloc.h头文件

功能:先按照newsize指定的大小分配空间,将原有数据从头到尾拷贝到新分配的内存区域,而后释放原来mem_address所指内存区域,同时返回新分配的内存区域的首地址。即重新分配存储器块的地址。

返回值:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。

注意:这里原始内存中的数据还是保持不变的。当内存不再使用时,应使用free()函数将内存块释放。

malloc()函数

原型:extern void *malloc(unsigned int num_bytes);

头文件:在TC2.0中可以用malloc.h或 alloc.h (注意:alloc.h 与 malloc.h 的内容是完全一致的),而在Visual C++6.0中可以用malloc.h或者stdlib.h。

功能:分配长度为num_bytes字节的内存块

返回值:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。当内存不再使用时,应使用free()函数将内存块释放。

说明:关于该函数的原型,在旧的版本中malloc返回的是char型指针,新的ANSIC标准规定,该函数返回为void型指针,因此必要时要进行类型转换。

calloc()函数

calloc是一个C语言函数

功 能: 在内存的动态存储区中分配n个长度为size的连续空间,函数返回一个指向分配起始地址的指针;如果分配不成功,返回NULL。

跟malloc的区别:

calloc在动态分配完内存后,自动初始化该内存空间为零,而malloc不初始化,里边数据是随机的垃圾数据。

用 法: void *calloc(unsigned n,unsigned size);

头文件:stdlib.h或malloc.h

#include <stdio.h>#include <stdlib.h>int main(void){     int num = 10;    int i;     long *p = (long *)malloc(num * sizeof(long));  long *p1=(long *)calloc(num,sizeof(long));

 for (i = 0; i < num; i++) {  printf("%d\t", p[i]); } for (i = 0; i < num; i++) {  printf("%d\t", p1[i]); }

 printf("内存地址: %p\n~~~~~~~~\n", p);   for (i = 0; i < num; i++)  p[i] = i+1;    for (i = 0; i < num; i++)   printf("%d\t", p[i]); 

 printf("\n------------------\n");    num = 4;     p = (long *)realloc(p, num*sizeof(long));   printf("内存地址: %p\n~~~~~~~~\n", p); for (i = 0; i < num; i++)   printf("%d\t", p[i]); 

 printf("\n------------------\n");     num = 10;     p = (long *)realloc(p, num*sizeof(long));   printf("内存地址: %p\n~~~~~~~~\n", p);   for (i = 0; i < num; i++)   printf("%d\t", p[i]);

 free(p);    free(p1); getchar();    return 0;}

运行结果为:

 

由数据可以很直观的看出他们之间的区别

输出全0说明calloc初始化时内存清零。

3 结构体初始化

typedef struct student //student可以省略{long num;char name[20];char sex;float score;} stu;

void use_fun1(void){   stu  stu_1; //定义stu结构体类型的变量 stu_1   stu  *p;p = &stu_1;stu_1.num = 89101;strcpy(stu_1.name, "Li Lin");stu_1.sex = 'M';stu_1.score = 89.5;printf("NO. :%ld\nname: %s\nsex:%c\nscore:%f\n", stu_1.num, stu_1.name, stu_1.sex, stu_1.score); printf("NO. :%ld\nname: %s\nsex:%c\nscore:%f\n", (*p).num, (*p).name, (*p).sex, (*p).score);printf("NO. :%ld\nname: %s\nsex:%c\nscore:%f\n", p->num, p->name, p->sex, p->score);}void use_fun2(void){stu  stu_1;stu  *p;p = (stu*)malloc(sizeof(stu));/*2.结构体指针需要初始化*/ ;p->num = 89101;strcpy(p->name, "Li Lin ");p->sex = 'M';p->score = 89.5;printf("NO. :%ld\nname: %s\nsex:%c\nscore:%f\n", p->num, p->name, p->sex, p->score);}

use_fun1和use_fun2两个函数输出都一样。

3.1结构体指针初始化

struct pep{  char *name;  int score;  struct pep* next;}sa,*sb; 

void use_fun3(void){  sa.name = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/    strcpy(sa.name,"Jimy");     sa.score = 99;       sb = (struct pep*)malloc(sizeof(struct pep));/*2.结构体指针需要初始化*/    sb->name = (char*)malloc(sizeof(char)); /*3.结构体指针的成员指针同样需要初始化*/    sa.next  = sb;     strcpy(sb->name,"Lucy");     sb->score = 98;     sb->next = NULL;     printf("name %s, score %d \n ",sa.name, sa.score);     printf("name %s, score %d \n ",sb->name, sb->score);     free(sb);     //释放内存}

0 0
原创粉丝点击