今天就到指针了
指针
指针是用来存放地址的。
# include <stdio.h>
int main()
{
int a=10, b=20;
int * pointer_1,* pointer_2;
pointer_1=&a; pointer_2=&b;
printf("a=%d and b=%d\n",a,b );
printf("*pointer_1=%d and *pointer_2=%d\n",*pointer_1,*pointer_2 );
return 0;
}
结果是:a=10 and b=20
*pointer_1=10 and *pointer_2=20
也就是说在pointer_1中存放的是a的地址。 在使用 * pointer_1时调用了其中的地址指向的变量。
对于引用指针变量:
- p=&a (p是一个指针)
- printf(“%d”, *p)。千万不要忘了“ * ”。 这是找到指针中地址指向变量。 如果没有那就是只输出地址了。
# include <stdio.h>
int main()
{
int a=10, b=20;
int * pointer_1,* pointer_2;
pointer_1=&a; pointer_2=&b;
printf("a=%d and b=%d\n",a,b );
printf("*pointer_1=%d and *pointer_2=%d\n",*pointer_1,*pointer_2 );
printf("*pointer_1_address=%ld and *pointer_2_address=%ld\n",pointer_1,pointer_2 );
return 0;
}
结果:
a=10 and b=20
*pointer_1=10 and *pointer_2=20
*pointer_1_address=140732920755528 and *pointer_2_address=140732920755524
- 赋值也可以 * p=1;
例子:输入两个数,按照大小顺序输出
# include <stdio.h>
int main()
{
printf("input two numbers: ");
int a,b;
scanf("%d %d",&a,&b);
int *p1,*p2;
p1=&a;p2=&b;
if(a>b)
printf("max is %d and min is &d\n",*p1,*p2);
else
printf("max is %d and min is %d\n",*p2,*p1);
return 0;
}
结果:
input two numbers: 19 35
max is 35 and min is 19
函数的参数不仅可以int float char 还可以是指针,这个作用就会说把一个变量的地址传送到另一个函数中。
同样是上面的例子:
# include <stdio.h>
void swap (int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int main()
{
void swap (int *p1, int *p2);
printf("input two numbers: ");
int a,b;
scanf("%d %d",&a,&b);
int *p1,*p2;
p1=&a;p2=&b;
if(a<b) swap(p1,p2);
printf("max is %d and min is %d\n",*p1,*p2);
return 0;
}
结果是:
input two numbers: 9 8
max is 9 and min is 8
通过指针引用数组
一个指针变量存放一个数组元素。数组元素的指针就是数组元素的地址。p=a;或者p=&a[0]a;都是把数租的第一个元素赋给指针p。这里理解为数组名a就是一个地址。所以可以p=a; p存放a数组首元素的地址。
例子:一个数组的元素用不同的方法输出
# include <stdio.h>
int main()
{
printf("input a array containing 5 values: ");
int a[5],*p,i;
p=a;
for (i=0;i<5;i++)
scanf("%d",p++);
printf ("the approach by using array is ");
for (i=0;i<5;i++)
printf(" %d",a[i]);
printf("\n");
printf("the appraach by using array_pointer is " );
for (i=0;i<5;i++)
printf(" %d",*(a+i));
printf("\n");
printf("the approach by using pointer is ");
p=a;
for (i=0;i<5;i++)
printf(" %d",*(p+i));
printf("\n");
printf("the approach by using pointer2 is ");
for (p;p<(a+5);p++)
printf("%d ",*p);
printf("\n");
return 0;
}
结果:
input a array containing 5 values: 1 2 3 4 5
the approach by using array is 1 2 3 4 5
the appraach by using array_pointer is 1 2 3 4 5
the approach by using pointer is 1 2 3 4 5
the approach by using pointer2 is 1 2 3 4 5
用指针引用字符串
有两种方法可以引用字符串:
- 用字符数组存放一个字符串。
- 用字符指针指向一个字符串。
#include <stdio.h>
int main()
{
char *string="hello, everyone!";
printf("%s\n",string);
return 0;
}
hello, everyone!
这里字符指针是指向字符串的第一个元素的地址。
例子:字符数组存放一个字符串,然后复制到字符数组b中
#include <stdio.h>
int main()
{
printf("input a string you want: ");
char a[100],b[100];
int i=0;
do
{
scanf("%c",&a[i]);
i++;
} while (a[i-1]!='\n');
a[i]='\0';
printf("string a is ");
for (i=0;*(a+i)!='\0';i++)
printf("%c",*(a+i));
for (i=0;*(a+i)!='\0';i++)
*(b+i)=*(a+i);
*(b+i)='\0';
printf("string b is ");
printf("%s\n",b);
return 0;
}
input a string you want: hello everyone! nice to see you here
string a is hello everyone! nice to see you here
string b is hello everyone! nice to see you here
至于字符指针做函数参数这一部分,对于指针的使用和之前的函数参数熟练地话,这也就是不是什么问题了。