Understanding Pointers in C Language

The concept and usage of pointers in C language play a crucial role throughout the learning process. Pointers can simplify certain C programming tasks, and some tasks, such as dynamic memory allocation, cannot be performed without pointers. Therefore, learning pointers is essential to becoming an excellent C programmer.As we mentioned, every variable has a memory location, and each memory location defines an address that can be accessed using the & operator, which represents an address in memory.Here is an example that outputs the address of a defined variable:

#include <stdio.h>
int main (){    int var_PLC = 10;    int *p;       // Define pointer variable    p = &var_PLC; // Assign address to pointer
   printf("var_PLC variable address: %p\n", p);   return 0;}

Next, let’s get to the point and learn about pointers:What is a pointer?A pointer is a memory address, and a pointer variable is used to store memory addresses. Just like other variables or constants, you must declare a pointer before using it to store the address of other variables. The general form for declaring a pointer variable is:

type *var_name;

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 a pointer * is the same as the asterisk used in multiplication. However, in this statement, the asterisk is used to specify that a variable is a pointer (var_name is a pointer, not *var_name). Below, I will write a few simple examples to help everyone correctly understand the declaration of pointer variables:

int    *ipoint;    /* An integer pointer */
double *dpoint;    /* A double pointer */
float  *fpoint;    /* A float pointer */
char   *cpoint;    /* A character pointer */

All actual data types, whether integer, float, character, or other data types, correspond to pointer values of the same type, which are long hexadecimal numbers representing memory addresses. The only difference between pointers of different data types is that the data type of the variable or constant pointed to by the pointer is different.Understanding Pointers in C LanguageThis statement may seem a bit abstract; please read it several times until you understand. After learning about pointer declaration, how to use pointers correctly becomes our primary task:How to use pointers?When using pointers, you will frequently perform the following operations: define a pointer variable, assign a variable address to the pointer, and access the value at the address stored in the pointer variable. These are done using the unary operator * to return the value of the variable located at the address specified by the operand. The following example involves these operations:

#include <stdio.h>
int main (){   int  var = 20;   /* Declaration of the actual variable */   int  *ip;        /* Declaration of pointer variable */   /* Remember: a pointer always points to the address of a variable or constant, not to the variable itself */   ip = &var;  /* Store the address of var in the pointer variable */
   printf("var variable address: %p\n", &var  );
   /* Address stored in the pointer variable */   printf("ip variable stored address: %p\n", ip );
   /* To get the value of the element at the memory unit pointed to by the pointer, just add * before the pointer */   printf("*ip variable value: %d\n", *ip );
   return 0;}

NULL Pointer in CWhen declaring a variable, if there is no specific address to assign, it is a good programming practice to assign a NULL value to the pointer variable. A pointer assigned a NULL value is called a null pointer. Besides null pointers, we often hear about wild pointers, which leads to the question: what is the difference between null pointers and wild pointers? Here, I have organized some differences for everyone to see:

Dimension Null Pointer Wild Pointer
Definition Explicitly assigned 0/NULL, pointing to “null address” Points to invalid or unallocated memory, address value is uncertain
Cause Explicit initialization/assigned NULL Uninitialized, not set to NULL after memory release, pointer out of bounds
Dereference Behavior Immediately triggers a segmentation fault, easy to locate May crash, may read/write garbage data, behavior unpredictable
Debugging Difficulty Easy: address fixed at 0x0 Difficult: address random or residual
Hazard Program crash, easy to fix Data corruption, security vulnerabilities, high concealment
Prevention Methods Check for null before use <span>if (p)</span> 1) Initialize to NULL

In most operating systems, programs are not allowed to access memory at address 0, as this memory is reserved by the operating system. However, memory address 0 has a particularly important meaning, indicating that the pointer does not point to an accessible memory location. By convention, if a pointer contains a null value (zero value), it is assumed that it does not point to anything.In C, there are many pointer-related concepts that are simple yet important. Below are some important concepts related to pointers that everyone must understand when learning C language pointers. You can look up relevant materials to learn more:

Concept Description
Pointer Arithmetic Four types of arithmetic operations can be performed on pointers: ++, –, +, –
Pointer Array An array can be defined to store pointers.
Pointer to Pointer C allows pointers to point to other pointers.
Passing Pointers to Functions Parameters can be passed by reference or address, allowing the passed parameters to be changed within the called function.
Returning Pointers from Functions C allows functions to return pointers to local variables, static variables, and dynamically allocated memory.

So, we will conclude our discussion on the basic concepts of pointers here. If you have any questions, feel free to discuss in the comments or message me. In the next lesson, we will learn about the role of pointers in functions and callback functions. You can preview the relevant content, and I will see you in the next class (づ。◕‿‿◕。)づ

Leave a Comment