C ++ learning 7th-array and pointer

Source: Internet
Author: User

1. array I

An array is an aggregate data type. Multiple variables of the same type are accessed through the array name and index.

Array access uses brackets and indexes to indicate variables. The index usually starts from 0;


The index of the array can be a non-constant integer value, but the length of the array must be a constant integer value;



The type of the array variable can be any type, including the basic data type and struct;

2. array II

1) array initialization. You can use parentheses to assign initial values to each variable, or assign values one by one. You can assign values to some variables:


2) omitting the array Length

Assign values in parentheses. The length of the array can be omitted. The default number of values is the length of the array;

You can use sizeof to calculate the number of Arrays:


3) array and enumeration:

The length and access index of the array can be expressed by enumeration:


3. arrays and for Loops

Arrays are most often used with for loops, such:

Common operations, including search value (maximum, minimum), calculation value (average, sum), and re-arrangement (from big to small, from small to big)


Note: exercise caution when crossing the border.

4. Sort the array by selection

Select the arrangement method: Start from 0, find the minimum value, and then exchange with 0. Then, start from 1, find the minimum value, and exchange with 1. iteration is performed in turn.


5. Multi-dimensional array

You can declare multi-dimensional arrays and assign initial values to parentheses. For two-dimensional arrays, the first-dimensional number can be omitted, but not both. The initial values can be uniformly assigned as 0, but pay attention to the dimension length.


Generally, two-dimensional arrays are accessed. Two variables are used. One is the row index and the other is the column index;

Multiple loops can be used to access two-dimensional arrays;

6. C-string

1) The previous cout can output strings:


In C and C ++, strings are considered as character arrays. Character arrays end with the ending symbol '\ 0', that is, the ASCI code 0.

Just like an ordinary array, once a character array is declared, its length cannot be changed;

You can use [] to change a character separately;

The character array follows all the rules of the array, such:


2) buffer and Buffer Overflow


When the number of characters entered exceeds the length of the character group, a buffer overflow occurs. A sufficient buffer size is required;

Generally, a space is used as the end input flag. You can enter a space as follows:


3) string operations

Including copy, connection, comparison, and length.

Strcpy (dststr, srcstr); // Overwrite

Strcat (dststr, srcstr); // an overflow exception occurs when srcstr is directly added to the end of dststr.

Strncat (dststr, srcstr, n); // only copy the first n characters of srcstr and add them to dststr. The buffer size is checked.

Strcmp (str1, str2); // if the values are equal, 0 is returned.

Strncmp (str1, str2, n); // compare the first n

Strlen (STR); // obtain the string length

4) standard string

STD: sring


You can use Getline to obtain the input with spaces:


7. pointer Introduction

1) pointer-the address that carries other variables.

For example:


When an asterisk is placed between the type and the pointer name, it declares a pointer variable. As for which side the asterisk is biased towards, everyone has their own habits. The asterisk does not belong to the name of the pointer variable.

Assign a value to the pointer variable. The value must be an address. You can use the get address operator-& to obtain the value: Point to/get the address.


The pointer variable type must match the executed variable type:


2) unreference pointer

A pointer points to an address. Another common practice is to take the value of this address and use-* to obtain the value pointed to by the pointer;


Therefore, pnptr is equivalent to & nvalue, while * pnptr is equivalent to nvalue;

Assign a value to * pnptr, just as assigning a value to nvalue:


In addition, pointers can be changed to other variables.

3) NULL pointer

NULL pointer-no pointer to any variable. The default address is 0;


In C, the NULL pointer is null, for example:


Null pointers are usually used in condition statements;

Null pointers are usually used in dynamic memory allocation.

4) pointer size (number of bytes)

The Pointer Points to the address of the CPU memory, and the number of bytes depends on the CPU machine, which is usually the same; for example, 32 CPUs, 32-bit pointers; and 64-bit CPUs, the pointer is 64-bit.

You can use sizeof (pointer name) to obtain the number of bytes of the pointer:


8. pointer, array, and pointer operations

In an array, the array name is essentially a pointer to the first element of the array:


2) pointer operation

In C, addition and subtraction operations can be performed on pointers;

Pnptr + 1 points the pointer to the next address in the memory. In addition, the returned address is not the next address of the current address, but the address of the next object after the current type;

For example, if the value of pnptr + 3 is 32-bit and the integer number is 4 bytes, the address after calculation is 12 bytes;

For example:


Most of the usage is ++. In the array, the array is accessed cyclically;

3) accessing arrays through pointer operations

Because * has a higher priority than +, you must add brackets to determine the priority;

For example:


9. Use new and delete for dynamic memory allocation


Generally, it is of great significance to dynamically determine the length of an array, which can be used effectively;

2) dynamically allocate memory space for a single variable


3) dynamically allocate memory space for Arrays


4) Memory leakage

Dynamic Memory is recycled until the program ends or is explicitly released;

If the memory is dynamically allocated, but the memory is not explicitly recycled, the memory is still occupied by the program, resulting in Memory leakage;

Programs occupy free memory.


5) NULL pointer II

It can also be understood as-no memory has been allocated.

You can determine whether the pointer is a null pointer and then determine the memory allocation;

Generally, the best habit is to set null pointers for non-assigned pointers:


10. pointers and constants

Like other variables, pointers can also be declared as constants. pointers and constants are easy to confuse. Use the keyword Const.

1) const is between * and the pointer variable name: the constant Pointer Points to an address when declared, and its value is immutable;


2) const is located before the type, so the pointer variable is a constant and cannot be changed:


However, when pointing to a non-constant pointer, you can re-point:


3) when the constant Pointer Points to a constant value, neither of them can be changed:


Constant pointers are often used in parameters passed to functions.

11. Reference

The alias of other variables ......; Use keywords &


Then & nvalue is equivalent to & rnref.

When declaring a reference, values must be assigned. Other values cannot be referenced again later;


2) constant reference. The reference value cannot be changed.


3) two typical applications referenced: Passing parameters by functions to prevent modifying the values of parameters; easy to access values in layers.

12. Reference, pointer, and member selection

1) references and pointers

A reference is like a constant pointer;


A constant reference is like a constant pointer pointing to a constant value;

Because the reference can only be valid memory, it is safer than the pointer;

2) select members

Select members. For example, the struct can be accessed through,

Because the priority of. is higher than *, brackets must be added;

In addition, C ++ provides-> for pointer variables to access members.

13. Void pointer

Void pointer, a generic pointer. It can point to any data type;

Note: Because the void pointer is not clear about its type in advance, it cannot be referenced through *. It must be accessed after explicit conversion:


Do not use the void pointer unless necessary.

[Disclaimer:
1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog !]

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.