定义:
A pointer in C is a way to share a memory address among different contexts (primarily functions). They are primarily used whenever a function needs to modify the content of a variable, of which it doesn't have ownership.
指针将需求的数据的存储器地址被分配给指针,并且可以在各种functions之间共享。
&取地址符
&val
returns the memory address of val
cin>>a;
cout<<&a<<endl;
将把val的内存地址分配给指针p 。
所以,当输入val=3时,输出指针p值仍为0x6ffe4c。
当要访问指针所指的内容时,就需要使用 *。
*p将返回VAL所反映的值,并且对它的任何修改都将反映在源头(val)上。
#include <stdio.h>
#include<math.h>
void update(int *a,int *b) {
int c=*a+*b;
int d=abs(*a-*b);
*a=c;
*b=d;
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
//hackerrank 实例