一些思考
关于变量值互换,我们考虑使用指针与不使用指针的差异。
#include<stdio.h>
void change(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int a = 10, b = 100;
int temp;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
a = 10;
b = 100;
change(&a,&b);
printf("a = %d, b = %d\n", a, b);
a = 10;
b = 100;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a = %d, b = %d", a, b);
getchar();
通过指针引用数组元素对比
#include<stdio.h>
int main()
{
int i;
int a[10];
printf("please input 10 numbers:");
for (i = 0;i < 10;i++)
{
scanf_s("%d", &a[i]);
}
for (i = 0;i < 10;i++)
{
printf("%d ", a[i]);
}
return 0;
}
#include<stdio.h>
int main()
{
int i;
int a[10];
printf("please input 10 numbers:");
for (i = 0;i < 10;i++)
{
scanf_s("%d", &a[i]);
}
for (i = 0;i < 10;i++)
{
printf("%d ", *(a+i));
}
return 0;
}
#include<stdio.h>
int main()
{
int i,*p;
int a[10];
printf("please input 10 numbers:");
for (i = 0;i < 10;i++)
{
scanf_s("%d", &a[i]);
}
for (p=a;p<(a+10);p++)
使p指向下一个元素
{
printf("%d ", *p);
}
return 0;
}
第一二种方法相同,但每次都需要计算元素的地址,费时较多 第三种方法比第一二种快,指针变量直接指向元素,不必每次都计算地址。 注意在循环中不用p变化的方法而用a变化的方法是不行的。如:for(p=a;a<(p+10);a++) a代表数组首元素地址,是指针型常量,值是固定不变的。a++无法实现
for(i=0;i<10;i++,p++)
printf("%d",*p)
for(i=0;i<10;i++)
printf("%d",*p++)
for(p=a;p<a+10;p++)
printf("%d",*p);
p=a;
while(p<a+100)
{printf("%d",*p);p++;}
for(i=0;i<10;i++)
scanf("%d",p++);
调转数字顺序
#include<stdio.h>
int i, j;
int main()
{
void cal(int* a, int n);
void cal2(int* a, int n);
int a[10] = { 1,3,5,2,3,6,7,8,5,9 };
int * p;
p = a;
for (i = 0;i < 10;i++)
printf("%d", *p++);
printf("\n");
cal(a, 10);
p = a;
for (i = 0;i < 10;i++)
printf("%d", *p++);
printf("\n第二种\n");
cal2(a, 10);
p = a;
for (i = 0;i < 10;i++)
printf("%d", *p++);
return 0;
}
void cal(int* a, int n)
{
int temp, m = (n - 1) / 2;
for (i = 0;i < m;i++)
{
j = n - 1 - i;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
void cal2(int* a, int n)
{
int* p, temp, * i, * j, m = (n - 1) / 2;
i = a;
j = a + n - 1;
p = a + m;
for (;i <= p;i++, j--)
{
temp = *i;
*i = *j;
*j = temp;
}
}
输出二维数组任一数值
#include<stdio.h>
int i, j;
int main()
{
int a[3][4] = { 1,3,5,7,9,11,13,15,17,19,21,23 };
int(*p)[4], i, j;
p = a;
printf("please input colunm and row:");
scanf_s("%d%d", &i, &j);
printf("a[%d,%d]=%d\n", i, j, *(*(p + i) + j));
return 0;
}
注意指向对象的基类型区别,p指向的是数组而不是元素。
p=a;//指向&a[0]
p=&a;//指向一维数组(即行)
(*p)[3];//指向行中序号为3的元素,如在上个例子中输入该式,
则会输出第3个元素:7(0为第一个元素)
int (*p)[4];//p的类型为int(*)[4]型,代表指向一维数组的整型变量,此一维数组有4个元素,长度为16个字节
*(p+2);//行运算得到&a[2][0],已经转化为指向列元素的指针了,若再加3;*(p+2)+3,则代表加了3*4个字节
score //score[0]的起始地址
*score //score[0][0]的起始地址。注意形参与实参的匹配
字符指针
int main()
{
char *string="hgkjjs ss";
printf("%s",string);
return 0;
}
将字符串a复制为字符串b(指针)
int i, j;
int main()
{
char a[] = "I am a student.", b[20];
int i;
for (i = 0;*(a + i) != 0;i++)
*(b + i) = *(a + i);
*(b + i) = '\0';//必须要复制字符串结束标志,要不会出现乱码
printf("string a is:%s\n", a);
for (i = 0;*(b + i) != 0;i++)
printf("%c", *(b+i));
printf("\n");
return 0;
}
将字符串a复制为字符串b(指针变量)
#include<stdio.h>
int main()
{
char a[] = "I am a boy", b[20], * p1, * p2;
p1 = a;
p2 = b;
for (;*p1 != 0;p1++, p2++)
*p2 = *p1;
*p2 = '\0';
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
return 0;
}
指针作为函数参数
字符数组名作为函数参数
#include<stdio.h>
int main()
{
void copy_string(char from[], char to[]);
char a[] = "I like math";
char b[] = "I like English";
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
copy_string(a, b);
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
return 0;
}
void copy_string(char from[], char to[])
{
int i = 0;
for (;from[i] != '\0';i++)
to[i] = from[i];
to[i] = '\0';
}
字符型指针变量作为函数参数
#include<stdio.h>
int main()
{
void copy_string(char from[], char to[]);
char a[] = "I like math";
char b[] = "I like English";
char* from = a, * to = b;
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
copy_string(from, to);
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
return 0;
}
void copy_string(char from[], char to[])
{
for (;*from != '\0';from++,to++)
*to = *from;
*to = '\0';
}
字符指针变量作形参和实参
#include<stdio.h>
int main()
{
void copy_string(const char *from, char *to);
const char *a = "I like math";
char b[] = "I like English";
char* p = b;
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
copy_string(a, p);
printf("The string a is:%s\n", a);
printf("The string b is:%s\n", b);
return 0;
}
void copy_string(const char* from, char* to)
{
for (;*from != '\0';from++, to++)
*to = *from;
*to = '\0';
}
a为字符指针变量,指向首字符。b是字符数组。p是字符指针变量,它的值是b数组首元素的地址。
字符指针变量和字符数组的比较
1.字符数组由若干个元素组成,每个元素中存放一个字符,而字符指针变量中存放的是地址。
2.可以对字符指针变量赋值,但不能对数组名赋值。
char *a;
a="ksdhks";
char str[14];
str[0]='s';
str="jkhkh";
3.初始化
char *a="hjkk";
等价于
char *a;
a="hjkk";
数组初始化
char str[14]="jsjjla";
不等价于
char str[14];
str[]="jsjjla";
4.存储单元内容
char* a;
scanf("%s",a);
正确:
char* a,str[10];
a=str;
scanf("%s",a);
5.指针变量的值可以改变,而字符数组名不能改变
char str[]={"jcsjisjsi"};
str=str+4;
printf("%s",str);
char *a={"jcsjisjsi"};
a=a+4;
printf("%s",a);
6.字符数组中各元素的值可以改变,但字符指针变量指向的字符串常量的内容不可以改变
char str[]="jcsjisjsi";
char *a="jcsjisjsi";
str[2]='r';
a[2]='r';
|