C pointer example, C pointer example

Source: Internet
Author: User

C pointer example, C pointer example

In computer science, Pointer is an object in programming languages. With an address, its value directly points to the value in another place in computer memory. Because the address can find the required variable unit, it can be said that the address points to the variable unit. Therefore, what visualizes an address is called a "Pointer ". It means that the memory unit with the address can be found.

In information engineering, a pointer is a variable used to indicate the language of a computer with a memory address or a central processor (CPU) Register (Register) [used to point to the variable or array corresponding to the memory address ]. Pointers usually appear in languages close to machine languages, such as assembly languages or C languages. Object-oriented languages such as Java generally avoid using pointers. Pointers generally point to a function or variable. When using a pointer, a program can directly use the memory address stored by this pointer, and can use the value of the function stored in this address.

This article focuses on Parsing pointers through several examples.

Array and pointer

1,

1         int a[5]={1,2,3,4,5};2         int *p=(int*)(&a+1);3         printf("%d\n",*(p-1));
/* A is an array name, that is, the first address of the array. Perform the address operator on a to get a pointer to the array !!!! This sentence is especially important! It is equivalent to int (* p) [5] = & a; p = & a + 1 pointing to the next int unit of Array Group a [4, a pointer pointing to a [4] = 5 p. It points to an array containing five int elements !! After p + 1 is executed, the offset of p is equivalent to p + sizeof (int) * 5 !! In the program, if the pointer p is forcibly converted into an int *, p-1 is actually p-sizeof (int). Therefore, p-1 points to the last element in the array, that is, 5 */

 

 

2,

        int a[]={1,2,3,4,5};        int*p[]={a,a+1,a+2,a+3};        int **q=p;        printf("%d\n",*(p[0]+1)+**(q+2));    
/* P [0] + 1 = a + 1; * (p [0] + 1) = 2; * (q + 2) = a + 2; * (a + 2) = 3; 3 + 2 = 5; where p [0] = a is the address of a [0, p [0] + 1 = a + 1 is the address of a [1], * (p [0] + 1) is the value of a [1] is 2, ** q = p --> * q = & p --> indicates that q stores the p address, q + 2 represents the p [2] address, and * (q + 2) indicates p [2], ** (q + 2) indicates * p [2] indicates * (a + 2) indicates that the value of a [2] is 3, so * (p [0] + 1) + ** (q + 2) = 5 */

3,

        int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};        int (*pt)[4]=a;        printf("%d\n",(*(pt+2))[2]);    

 

/* The two-dimensional array a [3] [4] can be viewed as {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }}; (* (pt + 2) [2] is equivalent to * (pt + 2) + 2) p [2] points to a [2], that is, the address of a [2] [0], so p [2] + 2 points to the address of a [2] [2, the content is 11. */

 

4,

        int a[3][2]={1,2,3,4,5};        int *p[3];        p[0]=a[1];        printf("%d\n",*(p[0]+1));
// * (P [0] + 1) indicates that the array element is a [1] [1].

5,

        static int a[]={5,4,3,2,1};        int *p[]={a+3,a+2,a+1,a};        int **q=p;        printf("%d\n",*(p[0]+1)+**(q+2));
        /*        *(p[0] +1 )  =  *( *(p+0) + 1) =  *(a+3 +1) = a[4]  = 1        **(q+2) =  *(*(q+2) +0 )  =*( p[2])  = *(a+1) = 4;
       */

6,

        int a[10]={1,2,3,4,5,6,7,8,9,10};        int *p=&a[3];        int b=p[5];        printf("%d\n",b);
/* A can be understood as the first address of this array. a [3] points to 4th elements, so p points to 4th elements. p [5] is the 5 after p, that is, 9th. so B = 9 */

7,

        int a[2][3]={1,2,3,4,5,6};        int *p;        p=a[0];        printf("%d\n",*(p+3));
// * (P + 3) indicates that the array element is a [1] [0];

 

Related Article

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.