}
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[] = "LongMai"; //定义并初始化数组str
char *pstr; //定义指针变量pstr
pstr = str; //使指针变量pstr指向数组str
//str = pstr; 这句话非法,是因为str是一个常量。而pstr是某个指针。
cout<<str<<"\n"<<pstr<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, num[3] = {11, 22, 33};
int *pnum;
pnum = num;
for(int i = 0; i < 3; ++i)
{
*pnum = i; //把i的值赋给pnum指向的内存单元
cout<<pnum<<endl; //可以看出来数组元素地址的变化
pnum++; //使pnum指向下一个数组元素
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char str[] = "China Beijing LongMai"; //定义数组str并初始化
char *pstr; //定义指针变量str
pstr = &str[6]; //初始化pstr,使指针变量pstr指向数组str
cout<<"str : ==== "<<str<<endl<<endl;
cout<<"pstr指向的str[6]:=== "<<*pstr<<endl;
return 0;
}
//数组指针的引用
#include <iostream>
using namespace std;
int main()
{
char title[] = "LongMain C Language pointer";
char *p_title;
cout<<"title : === "<<title<<endl;
p_title = title;
cout<<"p_title->title: "<<p_title<<endl;
cout<<"&title = %p \n"<<&title<<endl;
cout<<"p_title = %p \n"<<p_title<<endl;
cout<<"&title[0] = %p \n"<<&title[0]<<endl;
cout<<"======================="<<endl;
cout<<title[0]<<endl;
return 0;
}
#include <iostream>
#include <cstdio>
#define LEN 10
using namespace std;
int main()
{
char i;
char str[LEN] = "LongMai";
cout<<str<<endl;
for(i = 0; i < LEN; ++i)
{
cout<<str[i]<<"==========";
printf("%p\n", &str[i]);
}
return 0;
}
#include <iostream>
#include <cstdio>
#define LEN 26
using namespace std;
int main()
{
char str[LEN] = {'A', 'B', 'C', 'D', 'E', 'F', 'G','H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char idx, *p_str;
p_str = str;
printf("please input Idx(0 - 25) and Enter : \n");
scanf("%d", &idx);
if(idx < LEN)
printf(" The character is : %c \n", *(p_str + idx));
else
printf(" The Idx is overflow \n");
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char ver[] = "Gate Ver1.00 20100427";
char *p_ver;
p_ver = ver;
while(*p_ver)
{
printf("%c\t", *p_ver);
p_ver++;
}
printf("\n");
return 0;
}
/*#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char ver[] = "Gate Ver1.00 20100427";
char *p_ver;
p_ver = ver;
while(*p_ver)
{
printf(" %c ", *p_ver);
printf("%p", p_ver);
printf("\n");
p_ver++;
}
return 0;
}*/
//戏说指针数组
//类型说明符 *数组名[数组长度]
//*数组名: 表示该数组是指针数组
//数组长度: 表示数组元素的个数
#include <iostream>
#include <cstdio>
#include <stdio.h>
using namespace std;
int main()
{
char *str[ ] = {"China", "BeiJing", "LongMai"};
printf("str[0] = : %s\n", str[0]);
printf("str[1] = : %s\n", str[1]);
printf("str[2] = : %s\n", str[2]);
return 0;
}