1 / 19

函数申明、定义、调用

函数申明、定义、调用. 申明: void sort(float a[], int n); void sort(float *a, int m); void sort(float *a, int ); void sort(float *, int );. 函数申明、定义、调用. 定义: void sort(float a[], int n) { …… } void sort(float *data, int m) { …… }. 函数申明、定义、调用. float x[100], b[3][50]; double y[20];

aminia
Download Presentation

函数申明、定义、调用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 函数申明、定义、调用 申明: void sort(float a[], int n); void sort(float *a, int m); void sort(float *a, int); void sort(float *, int);

  2. 函数申明、定义、调用 定义: void sort(float a[], int n) { …… } void sort(float *data, int m) { …… }

  3. 函数申明、定义、调用 float x[100], b[3][50]; double y[20]; 调用:sort(x, 100); sort(x+10, 20); sort(b[0], 50); sort(b[2], 25); sort(b[2]+25, 25); sort(y, 20); ×

  4. 例10-7 输出命令行参数 在命令行状态下输入: echo How are you? How are you? 例11-7 编写C程序echo,它的功能是将所有命令行参数在同一行上输出。 #include <stdio.h> int main(int argc, char *argv[ ]) { int k; for(k = 1; k < argc; k++) /* 从第1个命令行参数开始 */ printf("%s ", argv[k]); /* 打印命令行参数 */ printf("\n"); return 0; }

  5. 指针数组和数组指针 指针数组 char *pa[5]; 类型名 *变量名[数组长度] 数组元素是指针类型,用于存放内存地址 每一个pa[i]都是指针,使用前必须先赋值,存放合理的内存地址 char (*ap)[5]; 类型名 (*变量名)[数组长度] 定义了一个指针变量,可以存放一个数组的地址。 例如 char a[5]; 那么, 可以 ap = &a; 而且 (*ap)[i] 等同于 a[i] char b[7][5]; ap = b; (ap指向了b的第一行) ap ++; (ap指向了b的第2行) ++ap; (ap指向了b的第3行)

  6. 指针数组和数组指针 char (*ap)[5]; 类型名 (*变量名)[数组长度] typedef类型名 MyType[数组长度] typedefchar CArray [5]; CArray *ap; CArray b[7]; //等同于char b[7][5]; ap = b; (ap指向了b的第一行) ap ++; (ap指向了b的第2行) ++ap; (ap指向了b的第3行)

  7. 指针与地址 地址就是一个32位的整数 void * addr; void指针可以强制转化为任何类型的指针,例如 (int*)addr (struct xxx*) addr void  像功能未定的干细胞 任何类型的指针都可以转化为void* 转化时,地址不变,对应的内存的内容不变

  8. 指针与地址 struct { int x, y[3]; } a[3] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}, *p; p = a+1; 表达式*((int *)(p+1)+2)的值为 11

  9. a[n-2] a[n-1] a[0] a[1] … … a[2] 排序算法 冒泡排序 选择排序 找出最大的元素,交换到数组最后 对剩下的n-1个算法重复执行 void sort( int a[], int n) /*选择排序的递归实现*/ { if( n==1 ) return; /* 只有1个元素,nothing need to do */ idMax 找到a[]中最大元素对应的下标 交换a[idMax]和a[n-1] sort(a,n-1); } 通过相邻元素交换,大的元素向后移 记录目前为止最大的元素的下标

  10. a[n-2] a[n-1] a[0] a[1] … … a[2] 查找算法 a0 < a1 < a2 <。。。< a[n-2] < a[n-1] 找元素x所在的下标 int find(int a[], int n, int x) { inti, k, m; } if(n==1) return (a[0]==m ? 0 : -1); m = n/2; if( a[m]==x ) return m; else if ( a[m]<x ) return find(a+m, n-m, x) else return find(a, m, x) for( k=0; k<n; k++ ) if( a[k]==x ) return k; 二分搜索 线性搜索

  11. 函数指针 double f1 ( double x ) { return (x*x); } double f2 ( double x ) { return (sin(x)/x); } double calc ( double (*f)(double), double a, double b ) { double z; z = (b-a)/2 * ( (*f)(a) + (*f)(b) ); /* 调用 f 指向的函数 */ return ( z ); } int main ( void ) { double result; result = calc(f1, 0.0, 1.0); /* 函数名f1作为函数calc的实参 */ printf("1: resule=%.4f\n", result); funp = f2; result = calc(funp, 1.0, 2.0); /* 函数指针funp作为函数calc的实参 */ printf("2: resule=%.4f\n", result); return 0; } 1: resule=0.5000 2: resule=0.6481

  12. 通过函数指针调用函数 int fun(x ,y) { return x > y ? x : y; } int (*funptr)( ); funptr = fun; • 调用函数 • 函数名 z = fun(3, 5); • 函数指针 (*funptr)(3, 5); (*函数指针名)(参数表)

  13. 字节 字节 . . . . . . . . . . . . . . . . 字节 字节 字节 字节 字节 字节 字节 字节 字节 字节 在磁盘上 文件 FILE *文件指针 在内存里 void * 指针

  14. 12.3.5 数据块读写fread()和fwrite() • fread(buffer, size, count, fp); 从二进制文件中读入一个数据块到内存 • fwrite(buffer, size, count, fp); 向二进制文件中写入一个数据块 • buffer:指针,表示存放数据的首地址; • size:数据块的字节数 • count:要读写的数据块块数 • fp:文件指针

  15. 文件打开方式 fp = fopen("f.txt", "r") • 文件打开方式参数表

  16. 12.2.3 关闭文件 if( fclose(fp) ){ printf( "Can not close the file!\n" ); exit(0); } fclose(文件指针) • 把缓冲区中的数据写入磁盘扇区,确保写文件的正常完成 • 释放文件缓冲区单元和FILE结构体,使文件指针与具体文件脱钩。 函数fclose() 的返回值 • 返回0:正常关闭文件 • 返回非0:无法正常关闭文件

  17. 文件读写函数 • 字符读写函数: fgetc / fputc • 字符串读写函数:fputs/ fgets • 格式化读写函数:fscanf/ fprintf • 其他相关函数: • 检测文件结尾函数feof • 检测文件读写出错函数ferror • 清除末尾标志和出错标志函数clearerr

  18. 其他相关函数 • 函数fseek( ) fseek(fp, offset, from); 用来控制指针移动 • offset:移动偏移量,long型 • from:起始位置,文件首部、当前位置和文件尾部分别对应0,1,2,或常量SEEK_SET、SEEK_CUR、SEEK_END。 例如: fseek(fp, 20L, 0):将文件位置指针移动到离文件首20字节处 fseek(fp, -20L, SEEK_END):将文件位置指针移动到离文件尾部前20字节处

  19. 其他相关函数 • 函数ftell( ) ftell(文件指针); 获取当前文件指针的位置,即相对于文件开头的位移量(字节数)。函数出错时,返回-1L 获取文件大小: fseek(fp, 0L, SEEK_SET); begin = ftell(fp); fseek(fp, 0L, SEEK_END); size = ftell(fp) - begin;

More Related