C和C++ , & 运算符告诉我们元素的地址。
元素前加 & 就可以知道地址。
//: C03:YourPets2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
using namespace std;
int dog, cat, bird, fish;
void f(int pet) {
cout << "pet id number: " << pet << endl;
}
int main() {
int i, j, k;
cout << "f(): " << (long)&f << endl;
cout << "dog: " << (long)&dog << endl;
cout << "cat: " << (long)&cat << endl;
cout << "bird: " << (long)&bird << endl;
cout << "fish: " << (long)&fish << endl;
cout << "i: " << (long)&i << endl;
cout << "j: " << (long)&j << endl;
cout << "k: " << (long)&k << endl;
getchar();
} ///:~
(long)是一种类型转换 cast。
意思是把他看成是long类型
main()内部定义的变量, 和外部定义的变量不在一个区域。
内存中代码和数据是分开存放的
dog cat bird 变量距离4个字节, 这台机器上,int占4个字节
C和C++有一个专门存放地址的变量类型,变量叫做指针
指针的运算符是 *, 但编译器知道他不是乘法
一个int指针
int* ip;
指针的作用, 函数内部改变 函数外面定义的变量。有更灵活的编程技巧。
输出
f(): 12718755
dog: 12775960
cat: 12775964
bird: 12775968
fish: 12775972
i: 5633980
j: 5633968
k: 5633956