C/c ++ variable, how much do you know, variable

Source: Internet
Author: User

C/c ++ variable, how much do you know, variable

First, I would like to ask: what is a variable?

Maybe, you tend to think about your way: Raise your head for 30 degrees, and move your right hand to the back of your head: "Hey, variable ......, a variable is a variable, for example, int a. a is an int variable ...... ".

Okay, the variable is the variable.


Variables are scattered in all computer programming languages like air. How much do you know about the variables in c/c ++? She is inseparable from you. How much do you know about her? This article explains what variables are in c/c ++.


The c/c ++ variable has three dimensions:

1. variable name

2. Variable address

3. Values in the variable address


How to understand the code first (programmers always like to read the code ):

# Include <iostream>

Using namespace std;

Int main ()

{

Int a = 2; // (I)

Printf ("& a = % d, a = % d \ n", & a, a); // (ii)

Return 0;

}

Running result:

& A = 0x2ef7a0, a = 2



In this Code, (I) defines the int type variable a and is initialized to 2. (Ii) print the address of a and the value of. From the results, we can conclude that:

1. a is the variable name.

2. The memory address of a is 0x2EF7A0.

3. The value stored in the memory address is 2.

Ps: What is the relationship between variable names and variable addresses?

In a metaphor: Jiangxi Agricultural University has its own longitude and dimension (115.839315, 28.768197) on the map, and (115.839315, 28.768197) is the address of Jiangxi Agricultural University, however, Jiangxi Agricultural University is named a, and Jiangxi Agricultural University contains students and teachers. These students and teachers are the values in the address,



Let's take a look:

# Include <iostream>

Using namespace std;

Int main ()

{

Int a = 2; // (I) normal variable

Int * pa = & a; // (ii) pointer variable

Int & ra = a; // (iii) reference variable

Printf ("& a = 0x % x, a = % d \ n", & a, a); // (iv)

Printf ("& pa = 0x % x, pa = % d, * pa = % d \ n", & pa, pa, * pa); // (v)

Printf ("& ra = 0x % x, ra = % d \ n", & ra, ra); // (vi)

Return 0;

}

Running result:

& A = 0x1ffe50, a = 2

& Pa = 0x1ffe44, pa = 0x1ffe50, * pa = 2

& Ra = 0x1ffe50, ra = 2

Our analysis code:

1. First, (I) (ii) (iii) defines different variables in 3

2. (I) as we have already said above, pointer variables also have three dimensions:

(1) pa is the variable name.

(2) pa has its own memory address (0x1ffe44)

(3) The value stored in the pa memory address is 0x1ffe50.

Ps: Compare the output results of (iv) and (v), we can better understand that the pointer is the variable that stores the address (the pa value is the address of ),


3. Some students have said that, the instructor, there will be no three dimensions for referencing variables. Well, let's first look at the definition of reference: Reference is the alias of the referenced variable (or object. How can we understand this sentence? It is still the analogy above: longitude and latitude (115.839315, 28.768197). This is called Jiangxi Agricultural University, but in Jiangxi Province, she also has a name: Jiangxi Agricultural University. Jiangxi Agricultural University and Jiangxi Agricultural University are in the same place, and this "Jiangxi Agricultural University" is the "Reference" variable name in our computer world. Therefore, variables referenced have three dimensions:

(1) ra is the reference variable name

(2) the address of ra is the address of.

(3) The value stored in the ra memory address is the value of a memory address I,



Some people said, teacher, I still don't understand what you mean.

Er ~~~, Okay, let's start with "practice:


# Include <iostream>

Using namespace std;

Void fun1 (int)

{

Printf ("(I): & a = 0x % x, a = % d \ n", & a, a); // (I)

A = a + 1;

}

Void fun2 (int *)

{

Printf ("(ii): & a = 0x % x, a = 0x % x, * a = % d \ n", & a, a, * ); // (ii );

* A = * a + 1;

}

Void fun3 (int &)

{

Printf ("(iii): & a = 0x % x, a = % d \ n", & a, a); // (iii );

A = a + 1;

}

Int main ()

{

Int a = 2;

Printf ("(iv): & a = 0x % x, a = % d \ n", & a, a); // (iv)

Fun1 (a); // (v)

Fun2 (& a); // (vi)

Fun3 (a); // (vii)

Return 0;

}


Above (I )~ (Iv) Several output results. Let's think about why, and whether a = a + 1 in each function changes the value of the real parameter.

Ps: to briefly describe the function call: When (v) calls fun1, the main function must save the current context data (not clear but Baidu, the real parameter value is also given to the form parameter, for example:

Fun1 (a) ;==> int a1 =;

Fun2 (& a); ==> int * a1 =;

Fun3 (a) ;==> int & a1 =;

(A1 is the form parameter of the function, indicating the difference)


From the code above, we can see the differences among value transfer, pointer transfer, and reference transfer in function calls.


Finally, let's ask another question: what is the variable?

Do you understand?


--------------

Public platform:

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.