Pointers
A pointer in C is a variable that stores the memory address of another variable. Pointers are used to access and manipulate the data stored at a specific memory location.
Pointers are declared using the * operator, which indicates that the variable is a pointer. For example:
int *p; // p is a pointer to an integer
float *q; // q is a pointer to a float
char *r; // r is a pointer to a char
To assign a memory address to a pointer, you can use the & operator to obtain the memory address of a variable. For example:
#include <stdio.h>
int main()
{
int a = 5;
int *p;
p = &a; // assign the memory address of a to p
printf("The memory address of a is: %p\n", &a);
printf("The value stored at the memory address of a is: %d\n", *p);
return 0;
}
In this example, we have declared an integer variable a with the value 5 and a pointer variable p that can store a memory address. The & operator is used to obtain the memory address of a, which is then assigned to p.
The printf function is used to print the memory address of a to the console, and the value stored at that memory address is printed using the * operator, which is used to dereference a pointer and access the value stored at the memory address it points to.
Pointers are a powerful and important feature of the C language, and they are used in many areas of programming. They allow you to manipulate data stored in memory and provide a way to pass data between functions.