Pointers in C
Pointer:
A pointer is a variable like a name that points to a storage location in memory (RAM). RAM has many cells to store values.
Pointer Declaration: A pointer is declared like a variable with an appropriate data type. The pointer variable in the declaration is represented by the * (asterisk) symbol. It has the following syntax:
type *v1, *v2,... *vn; // where type refers to the data type of the pointer and v1, v2,..., vn refers to pointer variables
Address Operator (&): The symbol & (ampersand) is an address operator which is used to access the address of a variable and assign it to a pointer to initialize it.
Example:
int m=10, *mptr; float x=3.14, *xptr; mptr=&m; xptr=&x;
Indirection Operator (*): The symbol * (asterisk) is an indirection operator which is used to access the value of a variable through a pointer.
Example:
int m=10, *mptr; float x=3.14, *xptr; mptr=&m; xptr=&x; printf("Value of m=%d", *mptr); printf("Value of x=%f", *xptr);