(4) Dearths from zero single row "C + + Primer" variables, references, pointers

Source: Internet
Author: User

From zero single row "C + + Primer"

--(4) variables, references, pointers


Initialization of variables

The initialization of a variable is a value that is given while creating a variable.


Initialization method:


int units_sold = 0;int Units_sold = {0};int Units_sold{0};int units_sold{0};long double ld = 3.1415926536;int A{ld}, B = { LD}; error:narrowing conversion requiredint C (LD), d = ld;//ok:but value would be truncated


Default initialization:


When we define a variable but do not initialize it, the variable is initialized by default. In general, a primitive type variable defined outside a function, the default initialization value is 0. Inside the function is the undefined. For a class structure, it is generally defined within the class.

Exercise 2.9:explain the following definitions. For those that is illegal, explain what's wrong and how to correct it.    std::cin >> int input_value; Error    int i = {3.14};              Error double to int    double salary = wage = 999.99;//error wage undefined    int i = 3.14;                  Ok

Exercise 2.10:what is the initial values, if any, of each of the following variables?std::string Global_str;//emptyint G Lobal_int; 0int Main (int argc, const char * argv[]) {    int local_int;//uninitialized    std::string local_str;//empty    Std::cout << local_str <<std::endl;    return 0;}
Declarations and definitions
in C + +, declarations and definitions are distinguished. A definition is also a declaration that allocates memory and assigns values to a variable at the same time as it is declared. The same name is defined only once, but it can be declared multiple times. C + + supports separate compilation. You may need the same variable in different files, and you need to use a declaration.

To declare but not define, use the keyword extern.

extern int i;//declares but does not define Iint J;  deckares and defines jextern double pi = 3.1416;  Definition then the extern will be covered out

Exercise 2.11:explain whether each of the following is a declaration or a definition: (a) extern int ix = 1024; Definition (b) int iy;            Definition (c) extern int iz; Declaration


Identifier

You can use letters, numbers, or underscores. However, you must start with a letter or underscore. Case sensitive.

Variable Naming conventions

1) naming should be meaningful.

2) usually lowercase letters

3) class name first letter general capitalization

4) Multiple words should be easy to distinguish, such as Student_loan or Studentloan


Variable name action range

Exercise 2.13:what ' s The value of J in the following program?int i = 42;int main () {int i = 100;int j = i;  100}
int i = +, sum = 0;    for (int i=0; i!=10; i++)        sum + = i;    std::cout<< i << "" << sum << std::endl;//100 45

Composite type
Reference:

Another name for an object when referencing. The reference must be initialized. A reference cannot re-reference a different object, and the reference is not an object.

Exercise 2.15 Which of the following definitions,if any,are invalid? Why? int ival = 1.01; int &rval1 = 1.01; Error int-double int &raval2 = ival; int &rval3; Error must be initialized

Exercise 2.16:which, if any, of the following assignments is invalid? If They is valid, explain what they do.int i = 0, &r1 = i; Double d = 0, &r2 = D; r2 = 3.14159;  valid r2 = R1;       Valid i = R2;        valid R1 = D;        Valid

Exercise 2.17:WAHT does the following code print?int I, &ri = I;i = 5; RI = 10;std::cout<< i << "" << ri << Std::endl; 10 10


Pointer:

When a pointer points to an object, you can use the introduction operator (*) to access the object.

int i = 42;int &r = i; &follows a type and is part of a declaration; R is a referenceint *p;     *follows a type and is part of a declaration;p is a pointerp = &i;     & is used in an expression as the address-of operator*p = i;     * is used in an expression as the dereference operatorint &r2 = *p; & is part of the Declaration; * Is the dereference operator

three ways to null pointers:

int *p = nullptr//equivalent to int *p1 = 0;int *p2 = 0;//must #include cstdlibint *p3 = NULL; equivalent to int *p3 = 0;

It is illegal to assign an int variable to a pointer.

int zero = 0;PI = zero; Error:cannot assign an int to a pointer

Note: All pointers should be initialized! If there is no pointer-bound object, it should also be assigned nullptr or 0.


other actions for pointers

1) as a condition

int ival = 1024;int *pi = 0;int *pi2 = &ival;if (pi)//PI has value 0,so condition evaluates as false    //...if (PI2) Pi2 points to ival,so it was not 0; The condition evaluates as true    //...

2) void* pointers

Void* is a special type of pointer that can hold any type of object address.

The void* pointer is used only to compare to other pointers, either as a function's return value, or as an assignment to other types of void* pointers.

Exercise 2.20:int i = 42;int *pi = &I;*PI = *pi * *pi;//I am evaluates to 1764

Exercise 2.21:explain Each of the following definitions.  Indicate whether any is illegal and, if so,why.int i =; (a) Double *DP = &i;//error (b) int *ip = I;//error (c) int *p = &i;//ok

Exercise 2.23:given A pointerp, can you determine whetherp points to a valid object? If So, how? If not, why not?

no, you can ' t. Why? Because it would is expensive to maintain meta data on what constitutes a valid pointer and what doesn ' t, and in C + + yo U don ' t pay to what do you don ' t want.

And you don't want to check whether a pointer are valid, because you know where a pointer comes from, EIT Her because it's a private part of your code, the control, or because you specified it in your external-facing Contrac Ts.


Pointer to pointer:

int ival = 1024;int *pi = &ival;int **ppi = &pi//ppi points to a pointer to an intcout << "the value of IVA L\n "       <<" Direct value: "<< ival <<" \ n "       <<" indirect value: ">> *pi <<" \ n "
   << "Double Indirect Value:" << **pi       <<endl;

Reference to Pointer:

int i = 42;int *p;int *&r = p;//R is a reference to the pointer pr = &i;*r = 0;


(4) Dearths from zero single row "C + + Primer" variables, references, pointers

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.