pointers, references, and values
What is a pointer? What is a memory address? What is called a pointer value? A pointer is a variable that stores the memory address of a computer. In this tutorial "reference" indicates the computer's memory address. Reading data from the memory pointed to by the pointer is called the pointer's value. Pointers can point to variable addresses of specific types, such as int, long, and double. The pointer can also be a void type, a null pointer, and an uninitialized pointer. All of these pointer types are discussed in this article.
Depending on where it appears, the operator * can be used to declare either a pointer variable or a pointer value. When used to declare a variable, * indicates that a pointer is declared here. Other cases use * to indicate the value of the pointer.
& is an address operator used to refer to a memory address. By using the & operator before the variable name, we can get the memory address of the variable.
Copy Code code as follows:
declare an int pointer
int *ptr;
declare an int value
int val = 1;
Assign a reference to an int value for the pointer
PTR = &val;
To value the pointer and print the content stored in the pointer address
int deref = *ptr;
printf ("%d\n", Deref);
Line 2nd, we declare an int pointer through the * operator. We then declare an int variable and assign a value of 1. We then initialize our int pointer with the address of the INT variable. Next, the int pointer is evaluated and the int pointer is initialized with the memory address of the variable. Finally, we print out the value of the variable, the content is 1.
The &val on line 6th is a reference. After the Val variable declares and initializes the memory, by using the address operator before the variable name & we can directly reference the variable's memory address.
Line 8th, we use the * operator again to value the pointer to directly obtain the data in the memory address that the pointer points to. Because the type that the pointer declares is int, the value that is taken is the int value of the memory address store that the pointer points to.
Here you can analogy the relationship of pointers, references, and values to envelopes, email addresses, and houses. A pointer is like an envelope, we can fill in the mailing address above. A reference (address) is like an email address, which is the actual address. The value is like the house where the address corresponds. We can erase the address on the envelope and write another address that we want, but this behavior has no effect on the house.
void pointers, null pointers, and uninitialized pointers
A pointer can be declared as a void type, such as void *x. A pointer can be assigned to NULL. After a pointer variable is declared but not assigned, it is called an uninitialized pointer.
Copy Code code as follows:
int *uninit; int pointer not initialized
int *nullptr = NULL; Initialize to NULL
void *vptr; void pointer not initialized
int val = 1;
int *iptr;
int *castptr;
A void type can store pointers or references of any type
Iptr = &val;
Vptr = iptr;
printf ("iptr=%p, vptr=%p\n", Iptr, vptr);
By displaying the transformation, we can turn a void pointer into a
int pointer and take a value
CASTPTR = (int *) vptr;
printf ("*castptr=%d\n", *castptr);
Print null and uninitialized pointers
printf ("uninit=%p, nullptr=%p\n", UnInit, nullptr);
Do not know what you will get the return value, will be a random garbage address?
printf ("*nullptr=%d\n", nullptr);
There's going to be a segment error.
printf ("*nullptr=%d\n", nullptr);
Execute the above code and you will get output similar to the following for different memory addresses.
Copy Code code as follows:
IPTR=0X7FFF94B89C6C, vptr=0x7fff94b89c6c
*castptr=1
UNINIT=0X7FFF94B89D50, nullptr= (nil)
Line 1th We declare an uninitialized int pointer. All pointers are uninitialized before the assignment is null, a reference (address), or another pointer. Line 2nd We declare a null pointer. Line 3rd declares a void pointer. Lines 4th through 6th declare an int value and a few int pointers.
Line 9th to 11, we assign the int pointer to a reference and assign the int pointer to a void pointer. A void pointer can hold various other pointer types. Most of the time they are used to store data structures. As you can see, line 11th we printed the address of int and void pointers. They now point to the same memory address. All the pointers store the memory address. Their type works only when the value is taken.
Line 15th to 16th, we convert the void pointer to an int pointer castptr. Note that you need to display the conversion here. Although the C language does not require a display to be converted, this increases the readability of the code. We then take a value of the castptr pointer, which is 1.
Line 19th is very interesting, where you print uninitialized pointers and null pointers. It is noteworthy that the uninitialized pointer has a memory address and is a spam address. I don't know what the value of this memory address is pointing to. This is why you should not take a value on an uninitialized pointer. At best, you get a spam address. Next you need to debug the program, and at worst, the program crashes.
The null pointer is initialized to O. Null is a special address, and a null-assigned pointer points to an address of 0 instead of a random address. This is only valid when you are ready to use this address. Do not take a value on a null address, or a segment error will occur.
Pointers and Arrays
The C-language array represents a contiguous memory space that is used to store multiple objects of a particular type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not of the same structure and therefore cannot be converted to each other. The array variable points to the memory address of the first element of the array.
An array variable is a constant. Even if the pointer variable points to the same address or a different array, you cannot assign the pointer to an array variable. It is also not possible to assign an array variable to another array. However, it seems puzzling that an array variable can be assigned to the pointer. When you assign an array variable to a pointer, you actually assign the address to the first element of the array to the pointer.
Copy Code code as follows:
int myarray[4] = {1,2,3,0};
int *ptr = myarray;
printf ("*ptr=%d\n", *ptr);
The array variable is a constant and cannot be assigned the following value
MyArray = ptr
MyArray = Myarray2
MyArray = &myarray2[0]
Line 1th Initializes an int array, and line 2nd initializes an int pointer with an array variable. Because the array variable is actually the address of the first element, we can assign the address to the pointer. This assignment is the same as the int *ptr = &myarray[0] effect, showing the first element address of the array to be assigned to the PTR reference. It should be noted here that the pointer needs to be consistent with the element type of the array, unless the pointer type is void.
Pointer and structure body
Like arrays, a pointer to a struct stores the memory address of the first element of the structure body. Like an array pointer, a struct's pointer must declare the same as the type of the struct or declare it to be of type void.
Copy Code code as follows:
struct Person {
int age;
Char *name;
};
struct person A;
struct person *ptr;
First.age = 21;
Char *fullname = "full name";
First.name = fullname;
PTR = &first;
printf ("age=%d, name=%s\n", First.age, Ptr->name);
Line 1th to 6th declares a person structure, a variable that points to a person structure and a pointer to the person structure. The 8th Behavior age member assigns an int value. Line 9th to 10th we declare a char pointer and assign the value to a char array and assign it to the struct name member. Line 11th We assign a person struct reference to a struct variable.
Line 13th we printed the age and name of the struct instance. Here are two different symbols to note, '. ' and '-> '. A struct instance can access the age variable by using the '. ' Symbol. For a pointer to a struct instance, we can access the name variable through the '-> ' symbol. You can also access the name variable by (*ptr). Name.
Summarize
Hopefully this brief overview will help you understand the different pointer types. In subsequent posts we will explore other types of pointers and advanced usage, such as function pointers.
Please ask questions and give comments.