32系统,指针大小为4字节
64系统,指针大小为8字节
- 代码实现:
#include <iostream>
using namespace std;
int main(int argc, char** argv, char* envp[])
{
// argc:传入参数的个数+1,第一个参数为主函数生成的可执行程序本身
// argv:传入的参数,有两种格式写法:char** argv; char* argv[]
// enpv:环境变量,最后一个值为空值
cout << "参数的个数:" << argc << endl;
for (int i = 0; i < argc; i ++ )
{
cout << "第" << i << "个参数是:" << argv[i] << endl;
}
for (int i = 0; envp[i] != 0; i ++ )
{
cout << "第" << i << "个环境变量是:" << envp[i] << endl;
}
// char char* char** char*[]
// char:定义一个字符
// char a[]:定义一个字符串数组,只能存放一个字符串
// char* a = "abc":定义一个字符串数组,只能存放一个字符串
// char** b ={"abc", "sdds"}:定义一个字符串数组,可以存放多个字符串
// char* b[]:同上
char a = 'a'; //定义一个字符变量,一个字节
std::cout << "a:" << a << std::endl;
// static_cast<void*>(&a):类型转换,void*是一种通用指针类型,不转换
// 输出会出错
std::cout << "&a:" << static_cast<void*>(&a) << std::endl;
char* a_p = &a; // 定义a_p,存放a_p的地址
std::cout << "a_p:" << static_cast<void*>(a_p) << std::endl;
std::cout << "a:" << *a_p << std::endl;
const char* b = "hello"; //定义一个字符串数组,另
const char** b_p = &b;
std::cout << "&b:" << &b << std::endl;
std::cout << "b_p:" << b_p << std::endl;
std::cout << "b[0]:" << b[0] << std::endl;
std::cout << "b:" << b << std::endl;
// 另外一种定义字符串数组的方法
char* c = (char*)"world";
char** c_p = &c;
std::cout << "c_p:" << c_p << std::endl;
std::cout << "&c:" << &c << std::endl;
std::cout << "c:" << *c_p << std::endl;
std::cout << *(c+1) <<std::endl;
const char* d[] = {b ,c};
std::cout << d << std::endl;
std::cout << *d << std::endl;
std::cout << *(d+1) << std::endl;
std::cout << d[0] << std::endl;
const char** e = d;
return 0;
}
- 运行结果:
参数的个数:1
第0个参数是:/home/mzb/slambook2/test1
a:a
&a:0x7ffe5c28ce43
a_p:0x7ffe5c28ce43
a:a
&b:0x7ffe5c28ce48
b_p:0x7ffe5c28ce48
b[0]:h
b:hello
c_p:0x7ffe5c28ce50
&c:0x7ffe5c28ce50
c:world
o
0x7ffe5c28ce70
hello
world
hello