C 语言中 const,指针,数组例子
来源:互联网 发布:视频剪切编辑软件 编辑:程序博客网 时间:2024/05/23 13:38
对函数形参使用const,在函数中不可以修改形参。
/*ryan-c*/#include <stdio.h>#define SIZE 10void show_array(const int ar[],int n);void mult_array(int ar[],int n,int mult);int main(void){int day[SIZE]={22,32,1,254,334,323,123,12432,324,43};int index;show_array(day,SIZE);mult_array(day,SIZE,5);show_array(day,SIZE);return 0;}void show_array( const int ar[],int n){int i;for(i=0;i<n;i++){//printf("%5d",ar[i]++);//形参声明为const,不能在函数中改变数组printf("%5d",ar[i]);}putchar('\n');}void mult_array(int ar[],int n,int mult){int i;for(i=0;i<n;i++){ar[i]*=mult;}}
22 32 1 254 334 323 12312432 324 43
110 160 5 1270 1670 1615 61562160 1620 215
当实参为const时,形参必须要const。
/*ryan-c*/#include <stdio.h>#define SIZE 10void show_array(const int ar[],int n);void mult_array(int ar[],int n,int mult);int main(void){int const day[SIZE]={22,32,1,254,334,323,123,12432,324,43};int index;show_array(day,SIZE);//mult_array(day,SIZE,5);//不允许,数组定义为const,形参必须也声明为constshow_array(day,SIZE);return 0;}void show_array( const int ar[],int n){int i;for(i=0;i<n;i++){//printf("%5d",ar[i]++);//形参声明为const,不能在函数中改变数组printf("%5d",ar[i]);}putchar('\n');}void mult_array(int ar[],int n,int mult){int i;for(i=0;i<n;i++){ar[i]*=mult;}}
jiang@ubuntujeos:~/c/20110825$ gcc aday.c -o aday.o;./aday.o
22 32 1 254 334 323 12312432 324 43
22 32 1 254 334 323 12312432 324 43
指向常量的指针不能用于修改数值。但可以指向其他地址
#include <stdio.h>#define SIZE 10int main(void){int day[SIZE]={22,32,1,254,334,323,123,12432,324,43};const int *ptr=day;//指向常量的指针printf("*ptr=%d\n",*ptr);//*ptr=28;//不允许改变指针指向的值day[0]=55;//day不是常量,所以可以修改printf("*ptr=%d\n",*ptr); ptr++; //可以让指针指向其他地址printf("*ptr=%d\n",*ptr);return 0;}
*ptr=22
*ptr=55
*ptr=32
将常量或非常量的数据地址赋给常量指针是合法的,但不可以通过常量指针修改数据。普通指针不能接受常量的地址。
/*hai836045106-c*/#include <stdio.h>#define SIZE 10int main(void){int day[SIZE]={22,32,1,254,334,323,123,12432,324,43};int const lockday[SIZE]={22,32,1,254,334,323,123,12432,324,43};const int *ptr;//指向常量的指针ptr=day; //可以指向非常量数据,不能修改printf("*ptr=%d\n",*ptr);ptr=lockday; //也可以指向常量数据,不能修改printf("*ptr=%d\n",*ptr); int *pnc;//普通的指针pnc=day; //可以指向非常量printf("*pnc=%d\n",*pnc);//pnc=lockday;//不可以指向常量return 0;}
jiang@ubuntujeos:~/c/20110825$ gcc aday.c -o aday.o;./aday.o
*ptr=22
*ptr=22
*pnc=22
常量指针与指针常量 (使用 const声明时必须初始化指针)
常量指针:指向常量类型地址的指针 const int * p //不可以修改数据,但可以指向别处
指针常量:指针类型的常量 int * const p; //可以修改数据,但不能指向别处
指向常量类型地址的的常量指针:const int *const p;//不可以更改地址,也不可以更改数据。
- C 语言中 const,指针,数组例子
- C语言中关于指针数组的一个例子
- C语言中Const指针变量
- c语言中const修饰指针变量
- C语言:指针数组、const指针+计算机系统:字节顺序
- C语言中函数指针的例子
- C语言中Const指针变量(常指针)
- C语言中const与指针的指针
- C语言中Const指针变量(常指针)
- c语言的指针、数组和常量修饰符const
- C中指针const
- C语言中数组越界的例子
- C语言中指针数组和数组指针的关系
- C语言中指针数组和数组指针
- C语言中数组指针和指针数组
- C语言中的const指针
- c语言初学-const指针
- const和指针(C语言)
- iphone开发——Transforms CTM, 转换矩阵
- 介绍utf8编码
- bundler_0.4在windows下的编译
- Ant将多个jar打包为一个jar
- 显式调用构造函数和析构函数
- C 语言中 const,指针,数组例子
- 8月19日Java培训小结
- 服务器开机自动发送短信到手机
- java 的泛型 2
- Android之getSystemService
- step-by-step多文件WEB批量上传(swfupload)的完美解决方案【zt】
- jstl中的<fmt:formatNumber>标签
- QT window下环境的搭建与编译第一个程序
- 长字符串解析--Strtok函数详解和实例