I. Definition and initialization of arrays1. Definition of Arraysthe dimensions of an array must be defined with a constant expression greater than or equal to one, which can contain only integer literal constants, enumerated types, or integers initialized with constant expressions. Non-const variables and const variables that do not know their values until run time cannot be used to define the dimensions of the array /** OK literal constant * /
int array[10];
/** OK Enumeration type */
Enum arraysize{
SIZE1 = 10,
SIZE2 = 12
};
int ARRAY2[SIZE1];
/** OK literal constant initialized const integer type */
const unsigned int arraySize = 10;
int array3[arraysize];
/** Error non-const integral type * /int arraySize = 10;
int Array[arraysize];//error:array bound is not an integer constant before '] ' token int array[arraysize];
/** error does not know the size of the array at run time * /const int arraySize = GetSize ();int array[arraysize];Note: This way at run time only know the notation of the dimension in g++ above compile no problem, but VC6 above compile does not pass, so it is recommended not to write like this (probably g++ version is much higher than VC6)2. Initialization of arrays
- A built-in array defined outside the body of a function whose elements are initialized to 0
- A built-in array defined within the function body, whose elements are not initialized
- The default constructor is called to initialize whether the class type array is defined within the function or outside the function (there must be a default constructor for the class type)
- When an array is explicitly initialized with an initialization list enclosed in {}, the number of elements in the initialization list cannot be greater than the size of the array, or the compilation error (Error:too many initializers)
- When an array is explicitly initialized with an initialization list enclosed in {}, the elements of the initialization list are less than the number of array elements, and the remaining elements are all initialized to 0
Note: The size of the array can no longer be changed once it is determined, if you want to insert elements into the input again, it is recommended to use a vector, or manipulate the memory, allocate a larger amount of memory (realloc)3. Operation of ArraysThe subscript of an array is generally of type size_t, which can cause "buffer overflow" If we forget to check if the subscript of the array is out of bounds during our programming process .
two. PointersWhat is a pointer? The pointer is used to point to the object! 1. Initialization of pointers and constraints on assignmentThe following four types of values can be used for initialization of pointers:
- Constant expression of 0 value (preferably initialized to null when defining pointers, wild pointers are scary)
- The address of the type-matching object
- The next address of another object
- Another valid pointer of the same type
Assigning a value of int to a pointer is illegal, but assigning a literal constant value of 0 or a const integer that is initialized to 0 (except for 0) is the right pointer! /** OK * /const int mynull = 0;int * ptr = mynull;/** error can only be 0*/const int mynull = 1;int * ptr = mynull;/** Error Type mismatch * /double dnumber;int * ptr = &dNumber;/** OK type cast to the same type of pointer * /double dnumber = 10.0;//Memory truncation here (8 bytes->4 bytes), according to the big-endian small end to point to Dnumber in memory high four bytes or low four bytes, once the PTR dereference, the resulting data is unpredictable! int * ptr = (int *) (&dnumber);
printf ("%d\n", *ptr);
2.void * PointerC + + provides a special kind of pointer that he can save any type of pointer/** OK * /int number = ten;double dnumber = 10.0;void * ptr = &number;ptr = &dNumber;Note: The void * Pointer indicates that he is associated with a memory address, but does not know the type of memory, so we cannot dereference the void * pointer and need to type-convert before the dereference! the void * Pointer only supports a few limited pointer operations:
- Make a comparison of two void * pointers
- Passing a void * pointer to a function or returning a void * pointer from a function
3. The difference between a pointer and a referenceAlthough pointers and references can access a value indirectly, there are some differences between them:
- A reference always points to an object (once it is initialized, it is always bound to the same particular object), no initialization is wrong when defining a reference, and the pointer does not point to any object (assignment is null)
- When assigning a value to a reference, the reference is made to the bound object, and when the pointer is assigned a value, the value of the pointer itself is modified, and the object pointed to by the pointer does not change
4. Accessing an array using pointersIn C + +, pointers and arrays are closely related, especially when using an array name in an expression, the array name is automatically converted into a pointer to the first element of the arraypointer and array subscript operations:We know that the subscript of an array is of type size_t, that is, the unsigned type, but it must be the same for pointers? Let's look at an example:int intarray[]={1,2,3,4,5};int * ptr = &intArray[2];
for (int i =-2; i < 3; ++i)
{
printf ("%d\n", Ptr[i]);
}
here the PTR subscript is 1-2 normal use, where the use of arrays using a relatively addressable method, PTR address +i offset, so this can be used relative addressing
5. Pointer to const and const pointer5.1 pointers to const objectsThe pointer to the const pointers is defined as follows:const int * PTR;indicates that the value of the object to which PTR is pointing cannot be modifieda Const object must be pointed to by a pointer to a const object, and not a const object can be pointed to by either a generic pointer or a pointer to a const object, which is a bit of a mouthful, let's look at some examples:/** OK */
const int number = 10;
const int * ptr = &number;
/** error number is a const object, you need to use the const pointer to point to */
const int number = 10;
int * ptr = &number;//If this is possible, then it is extremely dangerous to use PTR to assign a value to a const object!
/** OK */
int number = 10;
const int * ptr = &number;
int * ptr = &number;
Note the following : If you have a pointer of type void, you cannot point to the const object with void *, you must use the const void *!
5.2const PointerThe const pointer is defined as follows:int * const PTR;indicates that the value of PTR itself cannot be modified /** Error PTR value cannot be modified */ int number = 0;int * Const PTR = &number;ptr++;/** OK to modify the value of the object you are pointing to * /int number = 0;int * Const PTR = &number;*ptr = 10;//number = ten;
5.3 Const Pointer to const objectas the name implies, point to a const pointer, the pointer itself is a const type, the pointer to the object value can not be changed, its own pointer value can not be changed
6. Creating a dynamic arrayCreate a dynamic array general rules you know, let's look at some special cases:int * ptr = new int[-1];int * ptr = new int [0];What happens if the size of the requested array is less than or equal to 0?
int * PTR=NULL; try {ptr = new int[-1]; } catch (Std::bad_alloc e) {printf ("%s\n", "Bad Alloc"); } printf ("%p\n", PTR);
run the result asBad alloc00000000throws a Bad_alloc exceptionWhat if it's 0?
int * PTR=NULL; Try { ptr = new int[0]; } catch (Std::bad_alloc e) { printf ("%s\n", "Bad Alloc"); } printf ("%p\n", PTR);
run the result as00481818indicates that the allocation array succeeds at 0! Well, let's do a good job of assigning this to the inside.*ptr = 10;
printf ("%d\n", *ptr);
Output Tenoperation OK!!! In the "C + + Primer" above, it is not possible to do the dereference operation! Ask for explanation???
7. Multidimensional ArraysAs mentioned in "C Traps and defects", there is no so-called multidimensional array in C, except that the type of the array is an array, which means that the beginning of the multidimensional array is defined as:typedef int ARRAY_T[3];
array_t array[2];array is the same as defined as array[2][3];
The array and pointers of C + + check gaps