In the last tutorial, I mentioned pointers.
For pointers, this time I'll talk briefly about what I'm going to say later-you should have a pretty good foundation at that time.
First, the pointer type is spoken first.
Any type of keyword followed by a * symbol becomes a pointer type.
Like what:
char → char* character pointer
int → int* integer pointer
Double→double* dual precision pointer
You can even do this:
char*→pointer type of char** character pointer →pointer type of char*** character pointer ...
A pointer is essentially a memory address value that holds a value of the relevant type. However, the void* pointer type is the exception, and the memory address it points to is not a void value.
Because now the memory is very large, is always on the G. To illustrate the above scenario, I assume that there is a 16-byte memory such as:
Each lattice in the figure represents a byte of memory and can hold 8 bits
According to the previous tutorial, 1 char variable A, its size is 1 bytes. Can be placed in any one of the above, where I assume the first lattice is placed:
That is, if there is such a situation
Char a= ' z ';
The value of a ' z ' is stored in the first lattice
Then char* PA = &a;
Here the & operator indicates that the address of the variable A is taken.
Then the PA value must be 0.
If the variable A is in the second grid, then the PA value must be 1.
This means that the pointer is an address value, which is determined by the address of the run-time variable.
Because the pointer is also a value, the pointer variable also has a length.
In mobile phones and embedded environments, this address value is typically 4 bytes
In a PC environment, this value changes with the system:
WINDOWS:XP is 4 bytes in other systems, 32-bit programs are 4-byte 64-bit programs that are 8 bytes
Linux system: Ubuntu is generally 6 bytes (actually 8 bytes, only the middle two bytes will do the addition, the final synthesis of a 6-byte address, shape such as: 0xffff12345678 address value)
Mac system: With Linux
In some SCM, such as 51 single-chip microcomputer, PLC microcontroller, this value even only 2 bytes, that is, only 64k memory addressing range
In the memory environment I assumed above, this value is only half a byte 0x0~0xf
Since the pointer variable is a value, it is also in memory and has an address. To record this value, you can use a pointer to the pointer.
Continue with the assumptions above
char** PPA = &pa;
The value of adding PA is stored at 1
Then the PPA is equal to 1.
namely Pa=0 Ppa=1
Of course, the PA may not necessarily be stored in the 1 position, or in other locations depending on the actual operation of the program.
Because the pointer records the address value, there is a special pointer void*
This pointer records only the address value and does not exactly describe what type of data the address is pointing to.
But it also has great benefits for users who do not care about data types, and this pointer is very useful.
For example, the following system functions:
void* memset (void* dest,int val,int count);
The function is to place the memory of the specified dest position, populating Val until the count byte is filled
This function does not care about the type of address specified by the dest, char or int, double or, anyway, I'm going to fill in Val, until the count byte is filled.
So at this point, the void* pointer type is very good.
In view of this, all compilers provide a default convenience: that is, any type of pointer can be converted to the void* type, the compiler will not error
Like what
char* PA = &a;
memset (pa,1,1);
The compiler does not error, suggesting that you have a wrong pointer to the PA. Instead, it is silently converted to the void* type.
This section is primarily about pointers, and will continue to explain the pointers slowly. The pointer changes very much, the application is also very wide.
Ps:
The compiler also silently makes the following conversions:
char→int
Short→int
The unsigned version above unsigned char→unsigned int
Zerglurker's C language Tutorial 004--hands-On guide