Article directory
1. Array remaining knowledge
1. Multidimensional array
Two-dimensional array expression form: a[row subscript][column subscript]
#include<iostream>
using namespace std;
main()
{
//声明二维数组
int a[2][3],i,j;
//逐个输入(i为行,j为列)
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
cout<<endl;
//显示二维数组
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
//找出最大之元素及下标
int max=a[0][0],h=0,k=0;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>a[h][k])
{
h=i;k=j;max=a[i][j];
}
}
}
cout<<"\n max="<<max<<"下标为:"<<h"\t"<< k;
}
2. Character array
The character array type is "char"
#include<iostream>
using namespace std;
main()
{
char a[]={
'a','b','c','d'};
a[2]='\0'; //注意这里!
cout<<"\n a[0]="<<a[0];
cout<<"\n a[1]="<<a[1];
cout<<"\n a[2]="<<a[2];
cout<<"\n a[3]="<<a[3];
cout<<"\n a[4]="<<a[4];
cout<<"\n a="<<a;
}
The character variable will end the output when encountering '\0'. At the same time, the position is displayed as a space
By the way, I will give you the whole live L(=0 U 0=)/
#include<iostream>
using namespace std;
main()
{
char str[6];
cout<<"输入str=";
cin>>str;
//直接显示字符数组(普通数组不行)
cout<<"直接输出str:"<<str<<endl;
//
for(int i=0;i<6;i++)
{
cout<<endl<<"循环输出str:";
cout<<str[i];
}
cout<<endl;
//反向输出字符串
cout<<endl<<"反向输出str:";
for(int i=5;i>=0;i--)
{
cout<<str[i];
}
cout<<endl;
cout<<endl<<"大写输出str:";
for(int i=0;i<6;i++)
{
str[i]-=32;
cout<<str[i];
}
cout<<endl;
}
2. Getting started with pointers
1. Mr. pointer and address fetch operation
0: Definition of pointer concept
(NULL)
1: Declaration
type name of pointer variable *Pointer variable (to prevent misuse, we point the pointer variable to a null address NULL)
For example: int *fp1=NULL
__ __ _float fp2=NULL
__ ___char fp3=NULL
2: Get the address and get the content operation
operator "&" means to get the address of the right-hand variable
For example: & variable_
_ _ _ fp1=&n //fp1 gets the address of variable n
Note that pointer variables can only represent variables of the same type address, otherwise an error will be reported.
For example: int type *fp can only store int type variables, not float variables.
The operator "*" means to obtain the content of the pointer address at the right end, and it is the inverse operation of "&"
For example:
int a=100 *fp1;
fp1=&a;__________//Note: "fp1" indicates the address at this time
cout<<"*fp1="<<*fp1 //Note: "*fp1" indicates the value at this time
Result >>>*fp1=100
code show as below:
#include<iostream>
using namespace std;
main()
{
int a,b,c,*fp=NULL;
a=100;
fp=&a; //使指针指向变量a
cout<<"a="<<a<<endl;
cout<<"fp="<<fp<<endl;
cout<<"*fp="<<*fp<<endl<<endl;
fp=&b; //使指针指向变量b
b=200;
cout<<"b="<<b<<endl;
cout<<"fp="<<fp<<endl;
cout<<"*fp="<<*fp<<endl<<endl;
fp=&c; //使指针指向变量c
*fp=a+b;
cout<<"c="<<c<<endl;
cout<<"fp="<<fp<<endl;
cout<<"*fp="<<*fp<<endl<<endl;
//fp表示地址,需要用"&"取地址运算符
//*fp表示fp地址的值,需要用"*"取内容运算符
//地址都是用32进制储存(也有可能是64进制)
//int类型占用四个byte(字节),因此一同声明的变量a,b,c排列整齐
//赋值并不会影响变量的地址,因此fp取地址后依然可以运算该变量哦!
}
2. A pointer to an array
Pointers to arrays are common knowledge and can be expressed in the following ways:
- p=*a
- p=&a[0][0]
- p=*(a+0)
The following picture can be well represented: when expressing a complete two-dimensional array, the pointer changes as the array moves.
code show as below:
#include<iostream>
using namespace std;
/*上面内容复习:
"&"是取地址运算,"*" 是取内容运算
fp本质上是一个地址,
*fp可以作为内容被声明,赋值和运算 */
main()
{
int a[2][3]={
1,2,3,4,5,6},count;
//也可以用 a[2][3]={
{1,2,3},{4,5,6}}对多维数组赋值
int *fp;
/*以下这行代码是重中之重*/
fp=&a[0][0];//不可以用*fp=a[0][0]
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]="<<a[i][j]<<endl;
//可以用图片中的信息来解答下面这行代码 哦
cout<<"**a["<<i<<"]["<<j<<"]="<<*(fp+count)<<endl;
count+=1;
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]="<<&a[i][j];
cout<<"\t"<<a[i][j]<<endl;
}
}
}
3. Operation of pointer variables
It is better to understand it in combination with specific arrays here. waiting for perfection
#include<iostream>
using namespace std;
main()
{
int a[]={
1,2,3,4,5,6};
int *ip1,*ip2;
//指针的赋值运算
ip1=a;
ip2=ip1;
cout<<"ip1="<<(*ip1)<<endl;
cout<<"ip2="<<(*ip2)<<endl;
//指针的自增自减运算
ip1++;
ip2+=4;
cout<<"ip1="<<(*ip1)<<endl;
cout<<"ip2="<<(*ip2)<<endl;
//也可以进行布尔值运算
int n=ip2>ip1;
cout<<"ip2>ip1="<<n<<endl;
cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;
//指针之间也可以加减
n=ip2-ip1;
//记住是“ip2-ip1”而不是“&ip2-&ip1”。地址不可运算
cout<<"ip2-ip1="<<n<<endl;
}