Pointers to the knowledge base of data structures

Source: Internet
Author: User
Tags knowledge base

For many comrades who have studied C, they know that pointers are the most pristine place in C. Take the PHP language I am currently in the main, PHP this dynamic scripting language is also implemented with C, when we instantiate an object, the real process is also a stack in memory of the address point to the real object in the heap memory, that is, the real object is placed in the heap memory, The stack memory is placed in the address of the object in the heap memory, PHP is through the point of this address to implement an object instantiation process. It's a little bit above, and now it's about the definition of pointers and addresses.

Address: The address is the number in the memory unit, is a non-negative integer starting from 0, the range is related to our memory, if our memory is 4G, then the address range is 0-4g-1.

Pointers: In C, a pointer is a type, called a "pointer type," that describes the address. Simply speaking, the pointer is the address, the address is the pointer, the pointer variable is a variable that holds the address of the memory unit, essentially a non-negative integer with limited operation.

Here's a simple way to say a basic type of pointer.

I'm not going to say much, just the code:

  1  #include <stdio.h> 2   3  int  Main (void    4   5  int  *P;   int  i=  ;       int   J;      8   9  return   10 } 

The above is a basic C language code, from the above code we define a few variables, first of all, to define an I variable, and assign a value of 10, define a J variable, we do not assign a value.

The rest of the int *p is the focus, how can this be understood? As far as my own understanding is concerned, I understand that, as we see the * number, we know that p is a pointer variable that stores an address, and the preceding int indicates that the pointer variable p can only store the address of the data of type int.

So as explained above, we know that int *p is actually a pointer variable that can only store the address of the data of type int.

Then look at the following code:

1#include <stdio.h>2 3 intMainintargcChar*argv[])4 {5     int*p;6     intI=Ten;7     intJ;8     9p=&i;Tenj=*p; Oneprintf"j=%d", j); A      -      -      the      -     return 0; -}

In the above code, we have added 3 lines of code, where printf () is the output function, this is not explained. P=&i what does this line of code mean? We all know by the above explanation that P is actually a pointer variable that stores an int type of address, and in the principle that only variables of the same type can be assigned to each other, we can also know that &i should also be an address. In fact, the meaning of the "&" symbol is to take the meaning of the address, through which we get the address of the I variable.

The next line, J=*p, this line of code is also an assignment code, according to the principle of the left assignment, we know that *p is also an int type of variable, in the C language *p is to get the variable that the P variable points to, so *p is actually I variable, because I variable is 10, so, the J variable output is also 10. Is the result of the code running:

We will then understand from another direction:

From what we can see, the address of the variable in memory is not contiguous, it is randomly assigned by address bus, because p is a pointer variable, and by taking the operation of the address, point to the I variable, so the p pointer variable is stored 2000H, that is, the address of the I variable. The *p is represented as an I variable, and the J variable is made 10 by assigning a value to the J variable.

Again a more interesting thing, and then look at the code:

1#include <stdio.h>2 3 4 inthahaint*p) {5*p= -;6 }7 8 intMainintargcChar*argv[])9 {Ten     int*p; One     intI=Ten; A     intJ; -      -     //p=&i; the     //j=*p; -     //printf ("j=%d", j); -Haha (&i); -printf"i=%d", i); +      -      +      A     return 0; at}

, we define a haha () function where the parameter is a pointer variable p that points to the int type, remembering that our formal parameter is an address. So, when we call, we should pass in an address, so we pass in the &i, which is the address of the I variable. Through the above explanation, we should also know the final output, yes, 100:

Through the above study, we will also know, in fact, when we pass the reference is an address, so, the direct point is our variable, when we operate, that is, the assignment is directly to the address of the variable assigned value, so we have changed the value of the original variable i, so we output the output is 100.

The above is my understanding of the basic type of pointers, if there are errors in the place also please correct me, greatly appreciated!

This blog belongs to Bo Master original, if reproduced please indicate the source, thank you.

Pointers to the knowledge base of data 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.