goto,void,extern,sizeof分析
来源:互联网 发布:申万宏源手机交易软件 编辑:程序博客网 时间:2024/06/06 16:26
关键字的秘密
专题一:关键字的秘密。包括以下章节:
- 基本数据类型分析
- auto,register,static分析
- if,switch,do,while,for分析
- goto,void,extern,sizeof分析
- const,volatile分析
- struct,union分析
- enum,typedef分析
goto语句注意事项
不要使用goto语句:不符合程序设计思想(顺序执行的思想),容易出错。
void
- void表示无
- void在内存中没有定义固定大小
4-2.c
#include <stdio.h>int main(){ //gcc编译器处理结果为1;而g++却不能编译通过 //g++比gcc更严格的类型要求 printf("%lu\n", sizeof(void)); return 0;}
打印结果:
void*指针
- c语言规定只有相同类型的指针可以相互赋值
- void*指针作为左值用于接收任何类型指针的赋值
- void*指针作为右值需要进行强制类型转换
#include <stdio.h>#include <malloc.h>int main(){ int* pi = (int*)malloc(sizeof(int)); char* pc = (char*)malloc(sizeof(char)); void* p = NULL; int* pni = NULL; char* pnc = NULL; p = pi;//正确 pni = p;//编译错误(有些编译器不会出错,因为有些编译器不是那么严格。我们需要严格按照c语言标准,我们的代码才能有更好的移植性。) p = pc;//正确 pnc = p;//编译错误 return 0;}
手把手教你写代码:void*指针的使用
替换一个int类型的数组
4-4.c
#include <stdio.h>void* my_memset(void*p, char v, int size){ void* ret = p; char* dest = (char*)p; int i = 0; for(i = 0; i < size; i++) { dest[i] = v; } return ret;}int main(){ int a[5] = {5, 4, 3, 2, 1}; int i = 0; for(i = 0; i < 5; i++) { printf("%d\n", a[i]); } my_memset(a, 0, sizeof(a)); for(i = 0; i < 5; i++) { printf("%d\n", a[i]); } return 0;}
打印结果:
替换一个long类型的变量
#include <stdio.h>void* my_memset(void*p, char v, int size){ void* ret = p; char* dest = (char*)p; int i = 0; for(i = 0; i < size; i++) { dest[i] = v; } return ret;}int main(){// int a[5] = {5, 4, 3, 2, 1};// int i = 0;//// for(i = 0; i < 5; i++)// {// printf("%d\n", a[i]);// }// // my_memset(a, 0, sizeof(a));//// for(i = 0; i < 5; i++)// {// printf("%d\n", a[i]);// } //my_memset中参数p为void*,所以我们可以传任何类型的指针 long l = 10000; printf("%ld\n", l); my_memset(&l, 0, sizeof(l)); printf("%ld\n", l); return 0;}
打印结果:
extern
-extern用于声明外部定义的变量和函数
-extern告诉编译器用c方式编译
test4-1.c
#include <stdio.h>//告诉编译器以C语言方式编译//如果以gcc编译会报错,因为标准c语言中没有此用法extern "C"{ int add(int a, int b) { return a+b; }}//声明外部的变量和函数extern int g;extern int get_min(int a, int b);int main(){ int i = 5; int j = 4; printf("g=%d\n", g); printf("get_min=%d\n", get_min(i, j)); printf("add=%d\n", add(i, j)); return 0;}
test4-2.c
int g = 100;int get_min(int a, int b){ return (a<b)?a:b;}
打印结果:
为sizeof正名
-sizeof是编译器内置指示符,不是函数
-sizeof用于计算相应实体所占内存大小
-sizeof的值在编译期已经确定
#include <stdio.h>int main(){ int a; printf("%lu\n", sizeof(a)); printf("%lu\n", sizeof a); printf("%lu\n", sizeof(char)); //printf("%lu\n", sizeof char); //不能写成sizeof char //修饰int的只能是signed unsigned const,不能是sizeof return 0;}
打印结果:
阅读全文
0 0
- goto,void,extern,sizeof分析
- goto,void,extern,sizeof分析
- goto,void,extern,sizeof分析
- goto, void, extern, sizeof分析
- goto void extern sizeof
- 04-goto.void.extern.sizeof
- 1.4、goto、void、extern、sizeof剖析
- c语言学习笔记(2)goto,void,extern和sizeof分析
- 专题一关键字的剖析----4.goto,void,extern,sizeof的分析
- void,extern,sizeof 关键字分析
- 【C语言学习】04__goto,void,extern,sizeof分析
- goto void
- sizeof(void*)
- goto和void
- C/C++ sizeof(void)
- extern,inline,宏,sizeof
- sizeof extern数组
- void*与void;strlen与sizeof;memset
- ViewPager 的简单使用demo
- centos6.5安装boost_1_60版本需求
- 【BZOJ】3224 Tyvj 1728 普通平衡树 平衡树模板
- dede:arclist调用field全局变量{dede:global.cfg_templets_skin/}
- Activity之间的跳转的两种实现方法
- goto,void,extern,sizeof分析
- 页面打包下载功能(包括页面上带的附件)
- Android弹出多选框下拉框的实例
- thinkphp 下拉框保持默认值
- X度网盘大文件使用浏览器或迅雷下载的方法之一
- 微信小程序开发(一)-简易教程
- 定时请求php 后台 AJAX 请求,在页面中倒计时,和显示刷新时间
- Redux基础
- channelartlist标签增加orderway实现倒序