Learning Pointers in C is easy and fun. Pointers simplify the execution of some C programming tasks, and some tasks, such as dynamic memory allocation, cannot be performed without Pointers.
Therefore, to become a good C programmer, it is necessary to learn Pointers. So how do older programmers, who have been working for more than a decade, use Pointers?
Theoretical knowledge to see more than actual combat on the machine operation, through the project to master the actual application of pointer, 3 days with you to play pointer, want to attack pointer knowledge partner private letter I oh, by more than 10 years of programming experience to teach you the pointer of different high-level gameplay.
As you know, each variable has a memory location, and each memory location defines an address accessible using the hyphen (&) operator, which represents an address in memory. Take a look at the following example, which prints the address of the variable defined:
#include <stdio.h> int main () { int var1; char var2[10]; Printf (" address of var1 variable: %p\n", &var1); Printf (" address of var2 variable: %p\n", &var2); return 0; }Copy the code
When the above code is compiled and executed, it produces the following results:
Address of var1 variable: 0x7FFF5CC109d4 Address of var2 variable: 0x7FFF5CC109deCopy the code
From the above example, we learned what a memory address is and how to access it. Let’s look at what Pointers are.
What is a pointer?
A pointer is a variable whose value is the address of another variable, that is, the direct address of a memory location. Just like any other variable or constant, you must declare other variables before using Pointers to store their addresses. The general form of a pointer variable declaration is:
type *var-name;
Copy the code
Here, type is the base type of the pointer, which must be a valid C data type, and var-name is the name of the pointer variable. The asterisk * used to declare Pointers is the same asterisk used in multiplication. However, in this statement, the asterisk is used to specify that a variable is a pointer. Here are valid pointer declarations:
int *ip; /* a pointer to an integer */ double *dp; /* a double pointer */ float *fp; /* a float pointer */ char *ch; /* A character pointer */Copy the code
All real data types, whether integer, floating point, character, or any other data type, have the same type for the value of the pointer, which is a long hexadecimal number representing the memory address.
The only difference between Pointers of different data types is that the variable or constant to which the pointer points is of different data types.
How do I use Pointers?
When using Pointers, you frequently define a pointer variable, assign the address of the variable to the pointer, and access the value of the address available in the pointer variable. These are done by using the unary operator ***** to return the value of the variable at the address specified by the operand. The following example deals with these operations:
#include <stdio.h> int main () { int var = 20; /* Declaration of the actual variable */ int * IP; /* declaration of pointer variables */ IP = &var; Printf ("Address of var variable: %p\n", &var); /* The Address stored in the pointer variable */ printf("Address stored in IP variable: %p\n", IP); Printf ("Value of * IP variable: %d\n", * IP); return 0; }Copy the code
When the above code is compiled and executed, it produces the following results:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
Copy the code
NULL pointer in C
It is good programming practice to assign a NULL value to a pointer variable at variable declaration time if there is no exact address to assign. A pointer assigned to a NULL value is called a NULL pointer.
A NULL pointer is a constant defined in the standard library with a value of zero. Take a look at the following program:
#include <stdio.h> int main () { int *ptr = NULL; Printf (" PTR address is %p\n", PTR); return 0; }Copy the code
When the above code is compiled and executed, it produces the following results:
The address of the PTR is 0x0Copy the code
On most operating systems, programs are not allowed to access memory at address 0 because it is reserved by the operating system. However, memory address 0 is of particular importance, indicating that the pointer does not point to an accessible memory location. But by convention, if a pointer contains a null value (zero value), it is assumed that it points to nothing.
To check for a null pointer, you can use the if statement, as follows:
If (PTR) /* If p is not empty, then complete */ if(! PTR) /* If p is empty, complete */Copy the code
C Pointer details
In C, there are many pointer related concepts, all of which are simple but important. Here are some important pointer related concepts that C programmers should be aware of:
C function pointer
A function pointer is a pointer variable to a function.
A pointer pointer refers to a variable that points to an integer, a character, or an array. A function pointer refers to a function.
Function Pointers can be used to call functions and pass arguments just like normal functions.
Function pointer variable declaration:
typedef int (*fun_ptr)(int,int); // Declare a function pointer type that points to the same parameter and returns the valueCopy the code
The instance
The following example declares the function pointer variable p pointing to the function Max:
#include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int main(void) {/* p is a function pointer */ int (* p)(int, int) = &max; Int a, b, c, d; Printf (" Please enter three numbers :"); scanf("%d %d %d", & a, & b, & c); / * and direct calls the function equivalence, d = Max (Max (a, b), c) = p * / d (p (a, b), c); Printf (" The largest number is: %d\n", d); return 0; }Copy the code
Compile and execute, and the output is as follows:
Please enter three numbers :1, 2, 3 The largest number is 3Copy the code
The callback function mark
Function Pointers as arguments to a function
A function pointer variable can be used as an argument to a function. A callback function is a function called through a function pointer.
Simply put: a callback is a function that calls your implementation when executed by someone else’s function.
The instance
The populate_array function defines three arguments, the third of which is a pointer to the function that sets the value of the array.
In our example we define the callback function getNextRandomValue, which returns a random value that is passed as a function pointer to the populate_array function.
Populate_array calls the callback function 10 times and assigns the return value of the callback function to the array.
Void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) { for (size_t i=0; i<arraySize; i++) array[i] = getNextValue(); Int getNextRandomValue(void) {return rand(); } int main(void) { int myarray[10]; populate_array(myarray, 10, getNextRandomValue); for(int i = 0; i < 10; i++) { printf("%d ", myarray[i]); } printf("\n"); return 0; }Copy the code
Compile and execute, and the output is as follows:
16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709
Copy the code