C语言复习1之指针

来源:互联网 发布:shell脚本编程 题目 编辑:程序博客网 时间:2024/06/05 17:22


1.变量的属性

c语言中变量的地址和值的概念,比如:

<span style="white-space:pre"></span> int x=1;<span style="white-space:pre"></span> printf("x value:%d\n",x);<span style="white-space:pre"></span> printf("x address:%d\n",&x);

输出:

x value:1x address:1638212Press any key to continue


2 指针的定义:

假设 int *p=&a, 即int *p; p=&a;

则变量a的指针指的是变量a的地址;

p为指针变量,p的值即是指针变量的值,即变量a的地址;

*p为指针变量p指向的变量,即a,值跟a一样;

可能画图更好理解


代码实例:

#include <stdio.h>int main(){<span style="white-space:pre"></span> int x=1;<span style="white-space:pre"></span> printf("x value:%d\n",x);<span style="white-space:pre"></span> printf("x address:%d\n",&x);     int *y=&x;<span style="white-space:pre"></span> printf("y Pointer:%d\n",*y);<span style="white-space:pre"></span> printf("y value:%d\n",y);<span style="white-space:pre"></span> printf("y address:%d\n",&y);<span style="white-space:pre"></span>      <span style="white-space:pre"></span> int *z;<span style="white-space:pre"></span> z=&x;<span style="white-space:pre"></span> printf("z Pointer:%d\n",*z);<span style="white-space:pre"></span> printf("z value:%d\n",z);<span style="white-space:pre"></span> printf("z address:%d\n",&z);<span style="white-space:pre"></span> return 0;}

输出:

x value:1x address:1638212y Pointer:1y value:1638212y address:1638208z Pointer:1z value:1638212z address:1638204Press any key to continue


3.指针的指针:

指针的指针:如果一个指针变量存放的又是另一个指针变量的地址, 则称这个指针变量为指向指针的指针变量。

假设

 int **m; m=&y;

指针的指针m即指针y的地址;

*m为指针的指针m指向的变量y的值;

**m为指针的指针m指向的变量y所指向的变量的值:x

&m为指针的指针m的地址。


代码说明:

#include <stdio.h>int main(){<span style="white-space:pre"></span> int x=1;<span style="white-space:pre"></span> printf("x value:%d\n",x);<span style="white-space:pre"></span> printf("x address:%d\n",&x);     int *y=&x;<span style="white-space:pre"></span> printf("y Pointer:%d\n",*y);<span style="white-space:pre"></span> printf("y value:%d\n",y);<span style="white-space:pre"></span> printf("y address:%d\n",&y);<span style="white-space:pre"></span>      <span style="white-space:pre"></span> int *z;<span style="white-space:pre"></span> z=&x;<span style="white-space:pre"></span> printf("z Pointer:%d\n",*z);<span style="white-space:pre"></span> printf("z value:%d\n",z);<span style="white-space:pre"></span> printf("z address:%d\n",&z);     <span style="white-space:pre"></span> int **m;<span style="white-space:pre"></span> m=&y;<span style="white-space:pre"></span> printf("**m Pointer Pointer:%d\n",**m);<span style="white-space:pre"></span> printf("*m Pointer:%d\n",*m);<span style="white-space:pre"></span> printf("m value:%d\n",m);<span style="white-space:pre"></span> printf("&m address:%d\n",&m);<span style="white-space:pre"></span> return 0;}

输出:

x value:1x address:1638212y Pointer:1y value:1638212y address:1638208z Pointer:1z value:1638212z address:1638204**m Pointer Pointer:1*m Pointer:1638212m value:1638208&m address:1638200Press any key to continue

4.指针数组:

数组的指针即数组的地址,假设:

  int a[]={1,2,3,4,5};  int*p;  p=a;
则:

a[5]为数组,数组的收地址为a

将数组地址a的值付给数组指针变量p,则p跟a有一样的值.于是有:

p[i]=a[i]

*(p+i)=a[i]

&p[i]=&a[i]=p+i


代码实例:

#include <stdio.h>void parray(){  printf("Hello pArray\n");  int a[]={1,2,3,4,5};  int*p;  p=a;  printf("a=%d\n",a);  printf("p=%d\n",p);  for(int i=0;i<=4;i++){  printf("a[%d]=%d\n",i,a[i]);  printf("p[%d]=%d\n",i,p[i]);  printf("*(p+%d)=%d\n",i,*(p+i));  printf("p+%d=%d\n",i,p+i);  printf("&(p[%d])=%d\n",i,&(p[i]));  //printf("p[%d]=%d\n",i,p[i]);  =>error  //printf("&(p+%d)=%d\n",i,&(p+i)); =>error  }}
输出:
Hello pArraya=1638096p=1638096a[0]=1p[0]=1*(p+0)=1p+0=1638096&(p[0])=1638096a[1]=2p[1]=2*(p+1)=2p+1=1638100&(p[1])=1638100a[2]=3p[2]=3*(p+2)=3p+2=1638104&(p[2])=1638104a[3]=4p[3]=4*(p+3)=4p+3=1638108&(p[3])=1638108a[4]=5p[4]=5*(p+4)=5p+4=1638112&(p[4])=1638112Press any key to continue

5.多维数组指针和函数指针待续。













0 0
原创粉丝点击