C指针编程之道 ---第二次笔记

//数组的指针
#include <iostream>
using namespace std;
int main()
{
//定义一个int型数组并且初始化
int day[7] = {1, 2, 3, 4, 5, 6, 7};

//定义一个char型的数组并且初始化
char str[] = {"C language pointer"};


//数组的左臂是下标通过下标可以很随意的访问数组中的任意一个元素
int a = day[3];


///那么数组的右臂就是指针, 可以用引用法来引用数组
//接下俩的两种表达方式是相通的,
//都表示的数组的首地址,
//代表着属猪在内存中的存储位置。
int *b = day;
int *c = &day[0];
cout<<b<<endl;
cout<<c<<endl;
return 0;

}



#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;
}


你可能感兴趣的:(C指针编程之道 ---第二次笔记)