字符数组与字符指针
来源:互联网 发布:淘宝支付不了钱怎么办 编辑:程序博客网 时间:2024/05/16 04:56
一、错误程序:把a复制到b,内存不能为written
#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char *b = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b); } void copy_string( char *from, char *to ) { for( ; *from != '\0'; from++,to++) { *to = *from; } *to = '\0'; }
分析:char *b = "You are a student."; 相当于static char b[]="You are a student" , char *p; p=&b; b字符串被定义在常量区,故无法改写
故更改为char b[] = "You are a student."; b被定义在栈区,故能被改写
二、赋值方式
【错误】:char str[20]; str="1111111111"; //地址不能给一个数组
不等价于 char str[20]="11111111111";
【正确】:char *p; p="11111111111111";//date区的字符串"11111111111111"的首地址给了p指针
等价于 char *p="1111111111111";
三、字符输入
1、char str[10]; scanf("%s",str); 以上完全是可以的
2、char a*; scanf("%s",a); 以上也许可以,却是危险的。 因为a*没有被赋初值,不知道会指向哪里
四、指向函数的指针
1、指向函数的指针也可以作为参数,以实现函数地址的传递,这样就能够在被调用的函数中使用实参函数。
实参函数名 f1 f2
↓ ↓
void sub( int (*x1)(int),int (*x2)(int,int))
{ int a,b,i,j;
a=(*x1)(i); /*调用f1函数*/
b=(*x2)(i,j);/*调用f2函数*/
…
}
它的原理可以简述如下:有一个函数(假设函数名为sub),它有两个形参(x1和x2),定义x1和x2为指向函数的指针变量。在调用函数sub时,实参为两个函数名f1和f2,给形参传递的是函数f1和f2的地址。这样在函数sub中就可以调用f1和f2函数了。
2、“多态”函数代码
/* 第二次找出其中小者,第三次求a与b之和。 *//***********************************************************/#include <stdio.h>void main(){ int max(int, int); /* 函数声明 */ int min(int, int); /* 函数声明 */ int add(int, int); /* 函数声明 */ void process( int, int, int(*fun)() ); /* 函数声明 */ int a, b; printf("Endter a and b: "); scanf("%d %d", &a, &b); printf("max = "); process(a, b, max); printf("min = "); process(a, b, min); printf("sum = "); process(a, b, add);}int max(int x, int y) /* 函数定义 */{ int z; if( x > y ) { z = x; } else { z = y; } return z;}int min(int x, int y) /* 函数定义 */{ int z; if( x < y ) { z = x; } else { z = y; } return z;}int add(int x, int y){ int z; z = x + y; return z;}void process( int x, int y, int(*fun)() ) /* 函数定义 */ { int result; result = (*fun)(x, y); printf("%d\n", result);}
五、有关指针的数据类型的小结
定义
含义
int i;
定义整型变量i
int *p;
p为指向整型数据的指针变量
int a[n];
定义整型数组a,它有n个元素
int *p[n];
定义指针数组p,它由n个指向整型数据的指针元素组成
int (*p)[n];
p为指向含n个元素的一维数组的指针变量
int f();
f为带回整型函数值的函数
int *p();
p为带回一个指针的函数,该指针指向整型数据
int (*p)();
p为指向函数的指针,该函数返回一个整型值
int **p;
p是一个指针变量,它指向一个指向整型数据的指针变量
指针变量可以有空值,即该指针变量不指向任何变量,可以这样表示:p=NULL;
两个指针变量可以相减,如果两个指针变量都指向同一个数组中的元素,则两个指针变量值之差是两个指针之间的元素个数
void真正发挥的作用在于: (1) 对函数返回的限定; (2) 对函数参数的限定。 例如:void abc( void );
六、void指针 和 const指针
例如:void *p;表示指针变量p不指向一个确定的类型数据,它的作用仅仅是用来存放一个地址。
void指针它可以指向任何类型数据。也就是说,可以用任何类型的指针直接给void指针赋值。但是,如果需要将void指针的值赋给其他类型的指针,则需要进行强制类型转换。
- 字符数组与字符指针
- 字符数组与字符指针
- 字符指针与字符数组
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符指针与字符数组
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 字符数组与字符指针
- 自定义listview完成上拉刷新,下拉加载的功能,重写ontouchevent方法
- java-基础-Interface、abstract类、Static class 、non static class的区别
- Leetcode Find Peak Element
- 【项目成长日记】不懂事的SVN——This client is too old to work with the working copy at ()
- 想着想着
- 字符数组与字符指针
- 2016.7.29
- 给自己一个成功的理由
- android ndk 使用LOG
- 5-19 支票面额 (15分)
- cdoj柱爷与最大区间和
- xshell或secureCRT的中文乱码问题
- 定义 Java 中的方法
- 跟我学Redis(2)—Windows安装Redis