c_pointer

发布时间 2023-04-08 11:11:33作者: 哎呦_不想学习哟~
#include<iostream>
using namespace std;
    int f(int** r, int ** s){
        int temp= **r;
        int temp2=**s;
        int * z=*r;
        *r=*s;
        *s=z;
        printf("**r= %d\n",**r);
        printf("**s= %d\n",**s);
        *z+=3;
        **s-=8;
        **r-=19;
        return temp+ temp2;
    }

int main(void){
    int a =80;
    int b=12;
    int * p =&a;
    int * q= &b;
    int x=f(&p,&q);
    printf("x= %d\n",x);
    printf("*p= %d\n",*p);
    printf("*q= %d\n",*q);
    printf("a= %d\n",a);
    printf("b= %d\n",b);
    return 0;

}
**r= 12
**s= 80
x= 92
*p= -7
*q= 75
a= 75
b= -7

 

 

 

 

简单版:

#include<iostream>
using namespace std;
int main(){
    int a =80;
    int *p =&a;
    int **r=&p;
    cout<<"a: "<<a<<endl;
    cout<<"&a "<<&a<<endl;
    cout<<"*p=a: "<<*p<<endl;
    cout<<"p=&a: "<<p<<endl;
    cout<<"address of p:&p: "<<&p<<endl;
    cout<<"r=&p: "<<r<<endl;
    cout<<"*r=p=&a: "<<*r<<endl;
    cout<<"**r=*p=a: "<<**r<<endl;
    cout<<"address of r: "<<&r<<endl;
    return 0;
}
a: 80
&a 0x16fdfef58
*p=a: 80
p=&a: 0x16fdfef58
address of p:&p: 0x16fdfef50
r=&p: 0x16fdfef50
*r=p=&a: 0x16fdfef58
**r=*p=a: 80
address of r: 0x16fdfef48

总结:指针名称加上“&”后,才是自己真实的地址,而如果单单是指针名称,就是指针指向的变量的地址。

 

#include<iostream>
using namespace std;
int main(){
    int anArray[]={5,16,33,99};
    int * p=anArray;
    printf("*p= %d\n",*p);
    p++;
    printf("Now *p = %d\n",*p);
    int *q= &anArray[3];
    int ** x=&q;
    **x=12;
    *x=p;
    **x=42;
    q[1]=9;
    for(int i=0; i<4 ;i++){
        cout<<anArray[i]<<" ";
    }
    cout<<endl;
    return 0; 

}
*p= 5
Now *p = 16
5 42 9 12