[C/C ++ learning] pointers

Source: Internet
Author: User

[Cpp]
# Include <iostream>
Using namespace std;
 
Int main (void)
{
/*************************************** ********************
Pointer, memory address, pointer value, pointer
A pointer is a variable that stores the computer's memory address. The reference here is the computer memory address.
The read data from the memory pointed to by the pointer is called the pointer value.
Pointers can be void or other built-in data types.
There are also NULL pointers and Uninitialized pointers.
The "*" operator can be used to declare a pointer variable, or to dereference the operator. It can also be
Multiplication operator.
"&" Is the address operator. We can get the address of this variable by adding & to the variable.
**************************************** *******************/
 
 
Int * ptr; // declare an int type pointer.
Int value = 1; // declare an int type value. The value is 1.
Ptr = & value; // assign a reference to an int type value to the pointer.
Int * p = ptr; // value the pointer. Data in the memory address pointed to by the pointer can be obtained directly.
Cout <p <endl;
/*************************************** ***************************
Analogy: pointer to envelope to mailbox to house
OK. We can write the address (reference address) on the envelope. The value is equivalent to the address.
Corresponding house. We can also remove the address on the envelope and write other addresses we want.
The House remains unchanged, so it is not affected.
**************************************** ***************************/
 
 
/*************************************** ******************************
Below is the void pointer NULL pointer not initialized pointer
**************************************** *****************************/
Int * ptrs; // not initialized
Int * ptring = NULL; // NULL pointer
Void * vptr; // The void pointer is not initialized.
Int * iptr;
Int * vastptr;
// Void type can store any type of pointer or reference
Iptr = & value;
Vptr = & value;
Cout <iptr <vptr <endl;
// Convert the display type to convert a void pointer to an int pointer and take the value.
Vastptr = static_cast <int *> (vptr); // (int *) vptr;
Cout <* vastptr <endl;
 
// Cout <ptrs <ptring <endl;
/*************************************** ***************************
The uninitialized pointer also has a memory address, but it is a spam address.
Therefore, we cannot set uninitialized pointer values.
The best case is that you go to the spam address, and then you need to debug the program.
In the worst case, the program crashes.
**************************************** ***************************/
 
 
/*************************************** *****************************
An array is a continuous memory space used to store multiple specific types of objects.
A pointer is used to store a single memory address.
Therefore, arrays and pointers are not in the same structure and cannot be converted to each other.
The array variable is a constant, even if the pointer variable points to the same address or a different array,
You cannot assign a pointer to an array variable.
We can assign an array variable to the pointer, and assign the address pointing to the first element of the array to the pointer in the world.
 
Note that the pointer must be consistent with the array element type unless the pointer is of the void type.
**************************************** ****************************/
Int myarray [4] = {1, 2, 3, 0 };
Int * ptrarray = myarray; // * ptrarray = & myarray [0];
Cout <* ptr <endl;
// The operation above OK is correct. Let's look at the following errors.
// Myarray = ptrarray;
// Myarray = myarrays;
// Myarray = & myarrays [0];
 
/*************************************** ****************************
Struct and pointer. Similar to arrays, pointers to struct store
Memory Address. The pointer of the struct must be declared consistent with the struct type or be void.
**************************************** ***************************/
Struct person {
Int age;
Char * name;
};
Struct person first;
Struct person * ptrstruct;
 
First. age = 22;
Char * fullname = "full name ";
First. name = fullname;
Ptrstruct = & first;
 
Cout <first. age <ptrstruct-> name <endl;
 
 
Return 0;
}

View the running result:


June

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.