c语言,goto用法,goto最强使用方法
来源:互联网 发布:手机防御软件 编辑:程序博客网 时间:2024/04/29 08:54
1:先说goto的基本语法
#include<stdio.h>#include<stdlib.h>int main(){ int i = 1; while(1) { printf("在while(1)里\n"); while(i++) { printf("i = %d\n",i); if(i > 3) { goto TiaoChu; } } }TiaoChu: printf("程序结束\n"); return 0;}运行结果:标号位置在while(1)里234程序结束
从运行结果我们明显可以知道goto用法,可以跳出多重循环,标号只是标号,程序到标号位置正常执行。
2:goto语句有啥毛病,goto来回这么跳,在程序庞大后,在调试时很难找到错误,所以E.W.Dijikstra在1965年提出结构化程序设计来规避这种错误,也使得代码更容易阅读。
3:goto容易出错,但其仍然有存在的价值,在单个函数中使用goto基本不可能出错,goto在程序反操作上很好用
//函数成功返回1,失败返回0int fun(){ FIL *a,*b;//文件体 char *c,*d; a = fopen("***"); if(a = NULL) return 0; c = malloc(1000); if(c = NULL) goto _q1; b = fopen("***"); if(b = NULL) goto _q2; d = malloc(1000); if(d =NULL) goto _q3; return 1;q3: fclose(b);q2: free(c);q1: fclos(a); return 0;}
这种方式很方便的进行了反操作,而不用重复的去几次反操作。
我的实际例子:
/**函数功能:返回图片信息结构体p_inf* 参数:图片路径字符串picture_file_path* 返回值:返回图片信息,NULL时为读取图片信息失败* 作者:杨康*/p_inf *readPicInf(char *pfilepath){ FIL fileDescriptor;//文件体或者称文件描述符 uint32_t readByteResult;//作为f_read的最后一个参数,接受读到了多少个字节 char fOptResult;//接受文件操作返回值(也就是返回结果) p_inf *infReturn;//图片信息结构体,最后作为返回值 fOptResult = f_open(&fileDescriptor, (const TCHAR*)pfilepath, FA_READ); if ((fOptResult != FR_OK) || (fileDescriptor.obj.objsize > BMPMEMORYSIZE)) return NULL; infReturn = (p_inf *)malloc(sizeof(p_inf)); if (infReturn == NULL) goto INFRETURN_MALLOC_ERR; infReturn->pfilestring = (char *)malloc(fileDescriptor.obj.objsize); if (infReturn->pfilestring == NULL) goto INFRETURN_PFILESTRING_MALLOC_ERR; fOptResult = f_read(&fileDescriptor,infReturn->pfilestring,fileDescriptor.obj.objsize, (UINT *)&readByteResult); if ((fOptResult != FR_OK) || (readByteResult != fileDescriptor.obj.objsize)) goto F_READ_ERR; infReturn->pfilesize = fileDescriptor.obj.objsize; f_close(&fileDescriptor); return infReturn;F_READ_ERR: free(infReturn->pfilestring);INFRETURN_PFILESTRING_MALLOC_ERR: free(infReturn);INFRETURN_MALLOC_ERR: f_close(&fileDescriptor); return NULL;}
阅读全文
0 0
- c语言,goto用法,goto最强使用方法
- C语言中goto的使用方法
- C语言goto关键字
- C语言goto语句
- C语言goto语句
- C语言goto!!!!!!!!!!!!
- c语言 goto语句
- C语言 goto语句
- C语言中的goto
- C语言goto语句
- c语言 goto语句
- C语言goto语句
- C语言goto 语句
- C语言 goto 使用实例
- c语言的goto语句
- 【C语言】"if" "switch" "goto"
- 新人问题:C语言中goto的用法
- goto用法
- poi 写 2003,2007 excel
- 带你领会 线性代数 微积分的本质 3blue1brown 动画效果帅出天际
- Ubuntu16.04下安装OpenCV3.2.0
- linux 中 对文件权限的控制
- ConfigReader(四十一)—— ReadOtherItemConfig
- c语言,goto用法,goto最强使用方法
- IntelliJ IDEA 常用快捷键 之 Mac 版
- 依赖注入与控制反转
- struts2的知识点
- Eclipse中提交svn时过滤.project、.settings、.classpath等资源文件
- JSP编程
- ios-TextKit框架基础
- Python(六) 函数
- org.apache.commons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Se