Knowledge learning for pointers and structures

Source: Internet
Author: User

This week, I learned the most flexible technique in C-pointers. The pointer is also a variable, it has its own address and point to the address, in general, we are more concerned about the point of the pointer to the address. Pointers can be integers, floating-point numbers, characters, arrays, and the same can be a function.

The basic operation of a pointer is to assign, evaluate, take a pointer address, add an integer pointer to the pointer, increase the value of the pointer, decrease the value of the pointer, and the differential value. As shown in the following program:

1 /*PTR_OPS.C-pointer Operation*/2#include <stdio.h>3 intMain ()4 {5    inturn[5] = { -, $, -, -, -};6    int* PTR1, * ptr2, *PTR3;7PTR1 = urn;/*assign an address to the pointer*/8PTR2 = &urn[2];/*assigns an address to the pointer, obtains the value pointed to by the pointer, and obtains the address of the pointer*/9printf"pointer value, dereferenced pointer, pointer address: \ n");Tenprintf"ptr1 = 0x%x, *ptr1 =%d, &ptr1 = 0x%x", PTR1, *PTR1, &ptr1); Oneprintf"\nurn = 0x%x\n", urn); Aprintf"\nurn[0] = 0x%x\n", urn[0]); -    /*pointer addition*/ -PTR3 = ptr1 + 4;  theprintf"\nadding an int to pointer:\n"); -printf"ptr1 +4 = 0x%x,* (ptr1 + 3) =%d\n", PTR1 +4, * (PTR1 +3)); -ptr1++;/*Increment pointer*/ -printf"\nvalues after ptr1++\n"); +printf"ptr1 = 0x%x,*ptr1 =%D,&PTR1 = 0x%x\n",ptr1,*ptr1,&ptr1); -ptr2--;/*Decrement Pointer*/ +printf"\nvaluse after--ptr2\n"); Aprintf"ptr2 = 0X%X,*PTR2 =%D,&PTR2 = 0x%x\n",ptr2,*ptr2,&ptr2); at--PTR1;/*revert to initial value*/ -++PTR2;/*revert to initial value*/ -printf"\npointers Reset to original values: \ n"); -printf"ptr1 = 0X%X,PTR2 = 0x%x\n", PTR1,PTR2); -              /*one pointer minus another pointer*/ -printf"ubtracting one pointer from another: \ n"); inprintf"ptr2 = 0x%x,ptr1 = 0X%X,PTR2-PTR1 =%d\n", PTR2,PTR1,PTR2-ptr1); -              /*A pointer minus an integer*/ toprintf"\nsubtracting an int from a pointer: \ n"); +printf"PTR3 = 0x%x,ptr3-2 = 0x%x\n", PTR3,PTR3-2); -    return 0;  the     *}

Assignment: You can assign a value to a pointer, if it is an array, you can assign the value directly, if it is an integer, floating-point number or character, and so on with the address operator & to be assigned. When assigning a value, be sure to note that the type of the variable matches the type of the pointer, such as the address of the int type to be assigned to a pointer to int.

Fetch pointer Address: &PTR1 in the top program refers to the address of the PTR1 pointer variable, not the address that the pointer variable points to.

Add An integer to the pointer : The integer will be multiplied by the byte of the type that the pointer refers to. The resulting results are then added to the initial address. For example: PTR1 + 4 results are equivalent to &urn[4].

increase the value of the pointer: as with our self-add operation, add a pointer to an array element and let the pointer point to the next element of the array. For example ptr1++ makes PTR1 point to urn[1], but ptr1 the pointer's own address does not change, changing the point of the address.

subtract an integer from the pointer : similar to adding an integer to the pointer above, such as Ptr3-2 's result is equivalent to &urn[2], because PTR3 is pointing to &urn[4].

decrease the value of the pointer: similar to the value of the increment pointer.

Difference : The difference is calculated by calculating the difference between the two pointers in the same array to calculate the distance between two elements, for example: The value of PTR2-PTR1 is 2 and the distance between the two pointers refers to the two int numeric size.

comparison: You can compare the values of two pointers with the relational operator, but the two pointer types must be identical.

These basic things must be skilled to better use. At the beginning of the pointer knowledge is not understanding the pointer address and pointer to the address, there is the value of the pointer to the problem, and then through the programmatic debugging found that these basic things are defined, to remember, will be used on it.

In the later chapter of the C language of advanced technology, the knowledge of the structure, this piece in the example when the list of operations, delete nodes, find nodes, insert nodes and so on. After looking at the program and understanding the flow of the list, I think the linked list is a combination of pointers and structures. Each node in the list is a struct, including the value of the node and a pointer, which points to the address of the next node. I think it would be a good idea to understand this when you get to the point of the list.

1 /*Insert node Operation2 * First determine if the chain is an empty list. 3 * Create a new node that points to the value of the node to be inserted in the node structure's pointer to the node value. 4 * If the empty nodes in the linked list are not found. The pointer to the next node in the previous node of the empty node points to the new node. 5 * If it's an empty list, just treat the new node as the linked list header6 */7 8 intInsertintval)9 {TenNode p,q;/*define two nodes p and Q*/ Onep =head; A    if(P! = NULL) {/*find the right insertion position*/ -         while(P->next! = NULL) {/*at the end of the loop, the next node of P is exactly an empty node .*/ -p = p->Next; the        } -    } -Q = (Node)malloc(sizeof(structnode));/*Create a new node*/ -    if(q = =NULL) +       return-1; -Q->next = NULL;/*assign values to a new node*/ +Q->val =Val; A    if(p = = NULL) {//If the linked list is an empty list, the linked list will be inserted as the list header atHead =Q; -       return 1; -    } -P->next =Q; -    return 1; - } in //Delete node Operation - intDelintVal, Node *Res) to { + Node p, q; -p =head; the     if(p = =NULL); *       return-1; $     if(P->val = =val)Panax Notoginseng      { -*res =p; theHead = p->next;//The pointer to the next node in the Delete node is assigned to the link header, and the node is deleted . +          return 1;  A}Else if(P->next = = NULL)//There is only one node in the list, and it is not the node that we want to delete, return 1 . the         return-1;  +Q =p; -p = p->Next; $      while(P! =NULL) $     { -         if(P->val! = val) {//Locate the node you want to delete, assign a value to P for the node to be deleted, and assign a value to the previous node of the node that will be deleted to the Q . -Q =p; thep = p->Next; -}Else{WuyiQ->next = p->next;//Delete the node, the previous node of the node that will be deleted points directly to the last node to delete the node, which is the same as deleting the node theP->next = NULL;//the deleted node still exists, but not in this list, as a single node, pointing to an empty node . -*res =p; Wu              return 1;  -           } About    } $    return-1;  - } - //iterate through the list and print the value of each node. - voidprint () A { +Node p =head; the      while(P! =NULL) { -printf"%d\n",p->val); $p = p->Next; the     } the } the //iterate through the list and release each node. the voiddestory () - { inNode p =head; the      while(P! =NULL) { the Node Q; AboutQ =p; thep = p->Next; the        Free(q); the } +Head =NULL; -}

Above are insert nodes, delete nodes, print lists and release each node. I understand that the essence of a linked list is a struct, which holds the value of the list and the pointer to the next node. So the idea of inserting a node should be to find the place to be inserted, such as a, b, between two nodes. First assign the pointer to the next node in a to the pointer in the node you want to insert, point to B for the pointer you want to insert, and then modify the value of the pointer in a to point to the address of the node you want to insert. The above example of the program is written in the insertion pointer in a blank place, in fact, to be inserted into a number of the front and behind the idea is consistent, first find the location to insert, and then to modify the pointer can be. The same thing to delete a node is simply to point the pointer to the node at the end of the node, and then point the deleted node pointer to an empty address.

After watching the list operation, I have a specific understanding of the linked list, before just know that there is a linked list of this thing, but the list is exactly what kind of format, how to do not know how to operate. After reading the list, I also thought, the list is each node has a pointer, that if a node has two pointers, pointing to two addresses, it should be people said two fork tree. Think of this thing but not to practice, the next week there is time, you can write this program to try.

This is the second week of study, the second week of learning has a feeling is that learning code can not only be confined to the book, to learn to think, on the basis of practice, and then use practice to test their own thinking results. After all, the book is limited, it is impossible to cover everything, read the examples in the book at the same time to ask a few more if, if you modify what will be the result. Slowly learning process not only to knock the code every day, but also to slowly knock their code, can not follow other people's ideas, to put their own ideas with code to show, and then contrast to optimize, never knock code to knock code is the beginning, from knocking code to knocking their own code is the stage of Ascension, Slowly start to improve the quality of your code.



Knowledge learning for pointers and structures

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.