Latest Summary C language about pointers and other related understanding and use of matters

Source: Internet
Author: User
Definition: A pointer is a variable whose value is the address of another variable, and the address represents the location in memory. The thing to remember is that the array variable itself is a pointer.

Type of Address

The address is the type of existence, is not it feels strange, the pointer does not represent an address, the address has a type? See an example:

int    *ip;    /* A pointer to an integral type */double *DP;    /* A double-type pointer */float  *FP;    /* A floating-point pointer */char   *ch;     /* A pointer to a character type */

In fact, the pointer is always just a hexadecimal number representing an address, the so-called type, which refers to the type of the variable the pointer is pointing to.

Using pointers

How to define a pointer, the previous example should know, then how to print the hexadecimal address is how much and or what the pointer point to the data is how many:

The address of I was obtained by the & operator and saved to INTP int *intp; = &i;printf ("INTP stores the address:%p, the stored address points to the data:%d\n", INTP, *INTP);

Pointers can be operated: + + 、--、 +,-

In addition, pointers can be compared using relational operators such as = =, < and >

int intarr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};//first defines a pointer to execute the array first element int *INTARRP = &intarr[0];p rintf ("At this time INTARRP storage address is:%p, Data:%d\n ", INTARRP, *INTARRP);//Add a look at the result (each time it is added, it will point to the next integer position) intarrp++;p rintf (" INTARRP storage after the increment:%p, data:%d\n ", INTARRP, *INTARRP);

The pointer can also point to the pointer

int data = 5201314;int *P1 = &data;int **p2 = &p1;printf ("%d\n", data); Are 5201314printf ("%d\n", *p1);p rintf ("%d\n", **P2);

struct and pointer

The use of pointers in the structure is slightly special, mainly because of the particularity of the structure itself, want to use a value in the structure, generally divided into through the structure itself and pointers to the structure of the pointer two cases, see the following example:

struct node{    int val;    };/ /first build a struct data struct node Node;node.val = 1;struct node *nodep; Create a pointer to just the struct NODEP = &node;printf ("%d\n", nodep->val);//pointer to struct with->printf ("%d\n", node.val);//structure itself .

Related articles:

A detailed description of the application of ACTIVEMQ in C #

Related videos:

C Language Tutorials

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.