C + + pointers (as I know)

Source: Internet
Author: User

I do not know why, the recent is always quiet not to learn, and do not know what the reason, suddenly, I would like to learn a C + + (I think: "I am very interested in learning a thing, this is not a bad thing," but I still mainly Java), because very early before, when I have not touched Java, then , I have a choice to learn Java or C + + has one, what is it? is to learn that good, at that time, Java is quite popular, and C + + is more difficult to learn, especially in C + + pointers, many people say, quite complex, so I finally choose to self-study java.
These two days, learned the pointer, feeling is not like the legend of the touch (but it does a little scratching, because the pointer, often to a variable refers to), I think, this is to learn Java, understand the object-oriented meaning, if you understand what is the object, how to use, and understand the reference to pass, I think learning pointers is not difficult (if I'm wrong, don't laughed at) because the object is also in the memory address, and the reference is passed, always pointing to that memory address.

On my first C + + program
#include <iostream>
using namespace Std;


int Mainyw ();
int main ()
{
cout << "pointer arithmetic" << Endl;
int A, B;
int *pointer_1, *pointer_2; Pointer variable pointing to shaping variable
pointer_1 = &a; Take the address of the A variable
pointer_2 = &b;
*pointer_1 = 100;
*pointer_2 = 10;
cout << a << "" << b << Endl;
cout << *pointer_1 << "<< *pointer_2 << Endl;
MAINYW ();
return 0;
}


int Mainyw ()
{
int A, B, *p1, *P2, *p;
Cin >> a >> b;
P1 = &a;
P2 = &b;
if (a<b)
{
p = p1; P1 = p2; P2 = p;
}
cout << "A=" << a << "b=" << b << Endl;
cout << "max=" << *p1 << "min=" << *p2 << Endl;
return 0;
}


In the Mian method pointer_1 takes the address of the a variable, and then the pointer_1 pointer is assigned a value of 100, then the address that the pointer_1 points to is the address that the a variable points to, so

The result of the output is a=100,b=10, pointer_1 =100,pointer_2 =10.



Although AB is not re-assigned, the data changes because of the role of the address, so if a is modified to 90, the pointer variable pointer_1 is also

#include <iostream>using namespace std;
int main () {cout << "pointer arithmetic" << Endl;int A, B;int *pointer_1, *pointer_2; Pointer variable pointing to shaping variablepointer_1 = &a; Take the address of the A variablepointer_2 = &b;*pointer_1 = 100;*pointer_2 = 10;cout << a << "" << b << Endl;cout << *pointer_1 << "<< *pointer_2 << Endl;
cout << "Modify a variable" << Endl;a=90;cout << a << "" << b << Endl;cout << *pointer_1 << "<< *pointer_2 << Endl;return 0;}


we know that a function is to return a value (number, array or collection, object) at most, and now the myswap function can change the memory address of the two number passed in,
The two number of memory address interchange, is a to b,b into a, this is not equivalent to return a number of effects?

#include <iostream>using namespace std;
 void Myswap (int *p1, int *p2) {int t;T=*P1;*P1=*P2; Here, take the address.Interchange*p2=t;} int main () {int *point1,*point2,a,b;cout << "Input two numbers, compare size" << Endl;cin>>a>>b;cout<< "a=" <<a<< ", b=" <<b<<endl;point1=&a;point2=&b;if (a<b) {Myswap (point1, Point2);}cout<< "a=" <<a<< ", b=" <<b<<endl; cout<<*point1<< "," &LT;&LT;*POINT2&LT;&LT;ENDL;}


I initially entered the 2,5,a=2,b=5;MyswapThe two-digit address in a functioninterchange, when the pointer has changed, if the address of a is x000xf,bThe address is X004XF,
point1 The pointer turns out to be an addressx000xf, now, is pointing to thex004xf, andPoint2 pointed to thex000xf, the corresponding A.B value also changes because the address has changed, so the pointer is passed to thethe formal parameter of the Myswap is the reference pass.

Visible, the pointer is also a kind of how many wonderful things, it will let you programming the world more than an unexpected scenery (if you use good words)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + pointers (I know)

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.