Arrays, pointers and references

Arrays, pointers and references

Tags (separated by spaces): visual c ++


Array

definition

Array must be defined using the

data type array name [array size]

initialization

The size and the initial value of the specified array

int nArray[4]={1,2,3,4}

You can also use a number, to all the array initialization

int nArray[4]  = {-1}//nArray=[-1,-1,-1,-1]

Description thereof is omitted size of the array, the array initialization directly

int nArray[]={1,2,3,4}

The above array size is not specified, the length of the array is determined by the number of values inside the braces. Only when the array is immediately initialized when the can so

DESCRIPTION array size, and initializes the value of the first few

int nArray[4]={1,2}

To note the difference with the use of a number of initialization

pointer

definition

A pointer is a data type, a pointer variable having a type called a pointer variable.
**
pointer type * name * pointer
gives several common statement pointer variable

int * pn,*pi; //pn 和 pi是两个指向int型变量的指针
float *pl; //pl是指向float型变量的指针
char *pc;//pc是指向char型变量的指针
int *(pf)();//pf是一个指向函数的指针,该函数的返回值是int型数值
int * *pp;//pp 是一个指向指针的指针,即二维指针

initialization

Using the address-mark & obtained address of a variable, and then use this address to the pointer variable initialization

int a = 10;
int b[5] = {1,2,3,4,5}
int *pa = &a;
int *pb = b[3];

Use pointers

Storing a pointer address of a memory, a direct operation on the pointer, or change the memory address. According to the type of pointer changes will be different. For example, int type pointer subtraction of length 4, char type using subtraction length is 1. The value symbol * Gets the value stored in the address pointer.

int a[] ={10,20,30};
float b[] = {1.1,2.2,3.3};
char c[] = {'a','b','c'};
int *pa = a;
float *pb = b;
char *pc = c;
for(int i = 0;i<3;i++)
{
    cout<<"pa的存储的地址:"<<pa+i<<"\t,存储的数值是:"<<*pa+i<<endl;
    cout<<"pb的存储的地址:"<<pb+i<<"\t,存储的数值是:"<<*pb+i<<endl;
    cout<<"pc的存储的地址:"<<pb+i<<"\t,存储的数值是:"<<*pc+i<<endl;
    cout<<endl;
}

** corrected: you can see, the difference between char-pointers, plus or minus is also 4 **

Guess you like

Origin www.cnblogs.com/superxuezhazha/p/11424445.html