C Tutorial
C Language TutorialC IntroductionC Environment SetupC VScodeC Program StructureC Basic SyntaxC Data TypesC VariablesC ConstantsC Storage ClassesC OperatorsC DecisionC LoopsC FunctionsC Scope RulesC ArraysC enum(enumeration)C PointersC Function Pointers and Callback FunctionsC StringsC StructuresC UnionsC Bit FieldsC typedefC Input & OutputC File I/OC PreprocessorsC Header FilesC Type CastingC Error HandlingC RecursionC Variable ArgumentsC Memory ManagementC Undefined BehaviorC Command Line ArgumentsC Safe FunctionsC Sorting AlgorithmsC Project StructureC ExamplesC Classic 100 ExamplesC Quiz
C Standard Library β Reference Manual<a href="#" title="C Standard Library β ">C Standard Library β
C enum(enumeration)
C Function Pointers and Callback Functions
Deep Dive
Development Tools
Web Design and Development
Web Service
Software
Scripting
Programming
Programming Languages
Computer Science
Scripting Languages
Web Services
Learning C pointers is both simple and interesting. Through pointers, some C programming tasks can be simplified, and some tasks, such as dynamic memory allocation, cannot be performed without them. Therefore, to become an excellent C programmer, learning pointers is very necessary.
As you know, every variable has a memory location, and every memory location defines an address that can be accessed using the & operator, which represents an address in memory.
Look at the following example, which will output the address of the defined variable:
#include<stdio.h> int main(){ int var_tutorial = 10; int *p; // Define pointer variable p = &var_tutorial; printf("var_tutorial variable address: %pn", p); return 0; }
When the above code is compiled and executed, it produces the following result:
var_tutorial variable address: 0x7ffeeaae08d8
Through the example above, we understand what a memory address is and how to access it. Next, let's look at what a pointer is.
A pointer is essentially a memory address, and a pointer variable is a variable used to store a memory address. Just like other variables or constants, you must declare a pointer before using it to store the address of another variable. The general form of pointer variable declaration is:
type *var_name;
Here, type is the base type of the pointer; it 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. The following are valid pointer declarations:
int *ip; /* A pointer to an integer */ double *dp; /* A pointer to a double */ float *fp; /* A pointer to a float */ char *ch; /* A pointer to a character */
The type of the value of all actual data types, whether integer, floating-point, character, or other data types, is the same for the corresponding pointer: a long hexadecimal number representing a memory address.
The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
There are a few common operations that are frequently performed when using pointers: defining a pointer variable, assigning the address of a variable to a pointer, and accessing the value available 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; /* Actual variable declaration */ int *ip; /* Pointer variable declaration */ ip = &var; /* Store address of var in pointer variable*/ printf("Address of var variable: %pn", &var ); /* Address stored in pointer variable */ printf("Address stored in ip variable: %pn", ip ); /* Accessing the value using the pointer */ printf("Value of *ip variable: %dn", *ip ); return 0; }
Address of var variable: 0x7ffeeef168d8 Address stored in ip variable: 0x7ffeeef168d8 Value of *ip variable: 20
It is a good programming practice to assign a NULL value to a pointer variable in cases where you do not have an exact address to assign. A pointer that is assigned NULL is called a null pointer.
NULL is a constant defined in the standard library with a value of zero. Consider the following program:
#include <stdio.h> int main () { int *ptr = NULL; printf("The value of ptr is : %xn", ptr ); return 0; }
The value of ptr is : 0
On most operating systems, programs are not allowed to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it indicates that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains a null (zero) value, it is assumed to point to nothing.
To check for a null pointer, you can use an if statement as follows:
if(ptr) /* succeeds if p is not null */ if(!ptr) /* succeeds if p is null */
In C, there are many concepts related to pointers. These concepts are simple but very important. The following list outlines some important pointer-related concepts that every C programmer should be clear about: