C Language----pointers

Source: Internet
Author: User

I. Declaration
int  *p ;   //   int  * )( p )
Second, wild pointer--avoidance method

1) The pointer variable must be initialized to NULL, because any pointer variable that was just created will not automatically become a null pointer, and its default value is random.
2) When the memory space that the pointer p points to is released, the value of the pointer p is not set to null. Delete and free simply freed up the memory space, but did not assign the value of the pointer p to null. It is common to judge whether a pointer is legitimate, and whether the pointer is null by using the IF statement. For example:

int*p=newint(6);delete p;if(p!=NULL){*p=7;cout<<p<<endl;}

The correct wording should be the following code:

int*p=newint(6);delete p;p=NULL;if(p!=NULL){*p=7;cout<<p<<endl;}

So after allocating memory dynamically, if you use the dynamically allocated memory space, you must habitually use the delete operator to release it.

Third, struct pointer access member variable:
(*p).sex = ‘m’  p->sex = ‘m‘

(*p) is a struct variable after the pointer is evaluated. Sex is an access member variable use pointers to directly access the struct body
Member variable P--sex.

IV. structure arrays and pointers

The array name of the struct array is the struct pointer constant.

The array is characterized by: the array name is the first address of an array element;
The array name of the struct array is the first address of the element of the initial struct type;
A struct pointer is manipulated to manipulate an array of structures.

Five

Pointer to an array of n elements.

int  array[2][3* ( * ( p + 1 )  + 1 )  

int p[3] p is an array that has 3 elements, each of which is of type int, which is a pointer type that points to the integer data.

int a=10,b=20,c=30;int*p[3]={&a,&b,&c};

The p in int (*p) [3] is a pointer to an array that has 3 elements of type int. For example:

a[3]={1,2,3}; 那么p就是指向这个数组a的指针。int(*p)[3]=&a; // 这里赋值一定要用取地址符号。也就是取数组a的地址。

You cannot assign this value: Int (p) [3]=a;//Error: Type is incompatible. A is an array type and is not assignable to an int () [3] of this type.

But this is possible int p1=a;//OK because a can be implicitly converted to an int type whose value is actually the address of the first element of the array, which is &a[0]

*P[3] This is a pointer array which means that each element is equivalent to a pointer variable
and (*p) [3] p is a pointer variable representing a one-dimensional array that contains 3 integral elements

The former can be similar to a two-dimensional array to consider it to be composed of 3 one-dimensional arrays are often used in conjunction with strings to use a more convenient operation to put multiple strings in a pointer array
The latter is used to refer to a two-dimensional array of like a[2][3] This two-dimensional array we can use both pointers to reference and can be used (*P) [3] to refer to general usage (*P) [3]

Cases:

intMain () {intI,j;inta[2][3]={3,4,5,6,7,8};int*p[3] ;//Indicates that the storage is 3 integer variable address;int(*Q) [3];//Represents a one-dimensional array pointer to a 3 integer array element//Put the address of the first three elements in the P-pointer array. for(i=0;i<3; ++i) p[i]=&a[0][i];//The corresponding value of the address in the output pointer array for(j=0;j<3; ++j)cout<< *p[j]<<" ";cout<<endl;q=a;//Assign the array a start address to a one-dimensional array q; for(i=0;i<2; i++) for(j=0;j<3; j + +)cout<< * (* (q+i) +j) <<" ";//output array of elementsreturn 0;}
    • This article reference from Baidu Encyclopedia

C Language----pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.