c语言--指针

来源:互联网 发布:新疆网络干部学院网址 编辑:程序博客网 时间:2024/06/11 21:35

1.指针的概念

指针,也就是用来存储地址的变量。scanf()函数就是使用地址作为参数。 
一元运算符&可以取得变量的存储地址。假设a是一个变量,那么&a就是该变量的地址,一个变量的地址可以被看做是该变量在内存中的位置。

假设定义如下: 
a=24; 
并假设a的存储地址为0028FF1C 
那么printf(“%d %p”,pooh,&pooh); 
将输出24 0028FF1C

间接运算符 *

ptr=&bah 
这时就可以使用间接运算符*来获取bah中存放的数值 
val=*ptr

这两句放在一起等于下面 
val=bah

指针声明 
int * p;标识一个int类型的指针

我们可以通过如下来构建指针变量:

int * bah = & ptr;
  • 1
  • 1

我们可以这样想,计算机存储的时候需要两个东西,第一,数据的位置标识,第二,就是真正的值。通俗一点的说法,用key和value的形式来解释就是,bah的key是bah所对应的内存地址,而value则是ptr的对应地址。

2.指针对象比较

#include<stdio.h>void mikado(int);int main(){    int pooh=2;    int * p1= &pooh;    int * p2 = &pooh;    printf("pooh地址为%p\n",&pooh);    printf("p1=%d,p1地址为%p,p1指向地址%p\n",p1,&p1,p1);    printf("p2=%d,p2地址为%p,p2指向地址%p\n",p2,&p2,p2);    printf("%d",p1==p2);    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出如下

pooh地址为0028FF1Cp1=2686748,p1地址为0028FF18,p1指向地址0028FF1Cp2=2686748,p2地址为0028FF14,p2指向地址0028FF1C1
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

可见,两个指针==比较的时候是比较他们的value,也就是对应指向的pooh的地址,所以值为1,也就是true。

3.指针参数

#include<stdio.h>void mikado(int);int main(){    int pooh=2, bah=5;    printf("In main(), pooh=%d and &pooh= %p \n",pooh,&pooh);    printf("In main(), bah=%d and &bah= %p \n",bah,&bah);    mikado(pooh);    return 0;}void mikado(int bah){    int pooh=10;    printf("In mikado(), pooh=%d and &pooh= %p \n",pooh,&pooh);    printf("In mikado(), bah=%d and &bah= %p \n",bah,&bah);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

结果如下

In main(), pooh=2 and &pooh= 0028FF1CIn main(), bah=5 and &bah= 0028FF18In mikado(), pooh=10 and &pooh= 0028FEECIn mikado(), bah=2 and &bah= 0028FF00
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

在main函数中,pooh和bah是不同的值,因此有不同的地址。 
在mikado(pooh)的时候,只是进行了值传递,所以bah和main中的pooh也是不同的地址。

如果想将某个指针变量命名为ptr,可以使用如下语句 
ptr=&pooh 
ptr为一变量,pooh为一常量

这样我们修改上面代码就可以让mikado中的bah指向main函数中的pooh

#include<stdio.h>void mikado(int *bah);int main(){    int pooh=2, bah=5;    printf("In main(), pooh=%d and &pooh= %p \n",pooh,&pooh);    printf("In main(), bah=%d and &bah= %p \n",bah,&bah);    mikado(&pooh);    return 0;}void mikado(int  *bah){    int pooh=10;    printf("In mikado(), pooh=%d and &pooh= %p \n",pooh,&pooh);    printf("In mikado(), bah=%d and &bah= %p \n",*bah,&*bah);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

输出如下

In main(), pooh=2 and &pooh= 0028FF1CIn main(), bah=5 and &bah= 0028FF18In mikado(), pooh=10 and &pooh= 0028FEECIn mikado(), bah=2 and &bah= 0028FF1C
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如果mikado改为如下

void mikado(int  *bah){    int pooh=10;    printf("In mikado(), pooh=%d and &pooh= %p \n",pooh,&pooh);    printf("In mikado(), bah=%d and &bah= %p \n",bah,&bah);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

将有以下输出

In main(), pooh=2 and &pooh= 0028FF1CIn main(), bah=5 and &bah= 0028FF18In mikado(), pooh=10 and &pooh= 0028FEECIn mikado(), bah=2686748 and &bah= 0028FF00
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

有人可能会对这个输出有疑问,这是什么鬼东西。这里我们仔细想想,int *bah传递过来的是main函数中pooh的地址,这里是0028FF1C,我们在printf(“In mikado(), bah=%d and &bah= %p \n”,bah,&bah);第一个输出的是%d,是其值,我们通过计算器,将十六进制的0028FF1C转成十进制就是2686748,后面的输出是2686748的地址为0028FF00

4.数组中的指针

数组名同时也是该数组首元素的地址 
int a[12]; 
a==&a[0]; //这个判断是true

#include<stdio.h>#define SIZE 10int sump(int * start,int * end);int main(){    int marbles[SIZE]={11,10,5,22,4,16,19,32,31,20};    long anwser;    anwser=sump(marbles,marbles+SIZE);    printf("The total number of marbles is %ld.\n",anwser);    return 0;}int sump(int * start,int * end){    int total=0;    while(start<end)    {        total += *start;    //total为数组从第一个开始的值        start++;            //start++表示指向数组下个元素    }    return total;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

这里结果就是上面数组的和170

源码地址:https://github.com/oDevilo/C

0 0
原创粉丝点击