C + + Basics Tutorial-pointers

Source: Internet
Author: User

The use of pointers can directly manipulate the memory address, improve efficiency, the disadvantage is detours more difficult to understand, this tutorial for you to introduce the pointers in C + + language.

1, start Geany

1 point Menu "application-programming-geany" Start Geany, create a new C + + source program;

2 Point Menu "file-Save as" command, to "PTR" as the filename, save the file to its own folder;

2. Pointer variable

1 Use the pointer before you also define, with the * number to define, assignment with the address of other variables, can also be initialized to null;

Enter the following code:

int a = 25;

int *p = NULL;

p = &a;

2 The first sentence defines an integer variable A, each variable has a memory address, with the &a command to get the address of the variable,

The second sentence defines a pointer variable, the type is integral, the asterisk * is the definition symbol, and Null indicates that the assignment is null.

The third sentence is to assign a value to the pointer variable p, the value is the address of the variable A, formatted like 0x123456;

3) Then we will show the values of each variable, corresponding to their respective types;

cout << "a =" << A;

cout << "&a=" << &a;

cout << "p =" << p;

4 A is an integer variable, which is filled with integers, &a is the address operator, and gets the address,

P is a pointer variable, which holds the address, p gives the address of a, so two addresses are the same;

5 Next we look at using the pointer to manipulate its corresponding variable, continue to enter the following code;

cout << "*p=" << *p << Endl;

6 The *p here is not a definition pointer, because the asterisk * has no type before, and P has been defined and cannot be defined repeatedly,

The *p here is an indirect reference, pointing to A, because P holds the address of a, so the *p value is 25;

7 using *P can operate on variable A, for example, the following code assigns a value to A;

*p = 16;

cout << "a =" << A;

#include <iostream>

using namespace Std;

int main (int argc, char** argv)

{

int a = 25;

int *p = NULL; Defines the pointer variable and initializes the

p = &a; assigning to pointer variables

cout << "a =" << a << Endl;

cout << "&a=" << &a << Endl;

cout << "p =" << p << Endl;

cout << Endl;

cout << "*p=" << *p << Endl;

cout << Endl;

*p = 16; Indirect references, not defining pointers

cout << "a =" << A;

return 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.