A pointer variable must be declared before it is used. The declaration tells the compiler that the variable will store the address of a particular type of data. The * symbol in a pointer declaration indicates that the variable is a pointer.
A pointer variable must be declared before it is used. The declaration tells the compiler that the variable will store the address of a particular type of data. The * symbol in a pointer declaration indicates that the variable is a pointer.
Here, ptr is declared as a pointer to an integer.
It is meant to store the address of an int variable.
The general form of a pointer declaration is:
Each declaration creates a pointer for a different data type.
An integer pointer is used to store the address of an integer
variable. Its declaration uses the int data type
followed by * and the pointer name.
ptr stores the address of number.
A character pointer stores the address of a character variable.
The pointer is declared using the char type.
Here, ptr stores the address of the character
variable grade.
A floating-point pointer stores the address of a
float variable. It is declared using the
float type.
Here, ptr stores the address of
percentage.
Declaring a pointer only creates the pointer variable. Before using it to access a variable, it should be initialized with a valid address of a compatible variable.
The pointer ptr is initialized with the address of
number.
A pointer should normally be used with the corresponding data type it is declared for. This allows the compiler to correctly interpret the value stored at the pointed-to address.
Here, an int * pointer is used with an
int variable.
More than one pointer can be declared in the same statement. Every variable that needs to be a pointer must have its own * symbol.
Both p and q are pointers to integers.
A normal variable and a pointer variable can appear in the same declaration statement, but the * belongs only to the variable immediately associated with it.
Here, number is an ordinary integer variable,
whereas ptr is an integer pointer.
In a declaration such as int *ptr, the asterisk
indicates that ptr is a pointer to an integer.
It does not mean that ptr will store an ordinary
integer value.
This means ptr is capable of storing the address
of an integer value.
| Declaration | Meaning |
|---|---|
int *p; |
p is a pointer to an integer. |
char *p; |
p is a pointer to a character. |
float *p; |
p is a pointer to a floating-point value. |
double *p; |
p is a pointer to a double value. |
Write a C program to declare an integer pointer, store the address of an integer variable in it, and display the original value and the value accessed through the pointer.
Declare an integer variable and an integer pointer. Assign
&number to the pointer and use *ptr
to access the stored value.
A pointer declaration specifies the type of value whose address the pointer is expected to store. After initialization, the pointer can be used with the indirection operator to access that value.
Value of number = 50
Value through pointer = 50
The pointer ptr does not contain the value
50 directly. It contains the address of
number, and *ptr accesses the value
stored at that address.
Declaring a pointer and using it before giving it a valid address can lead to undefined behavior.
Do not confuse int *ptr with
*ptr. The first is a declaration, while the second
is used to access the value at the stored address.
| Concept | Remember |
|---|---|
| Pointer Declaration |
data_type *pointer_name;
|
| Integer Pointer |
int *p;
|
| Character Pointer |
char *p;
|
| Initialization | Store a compatible variable's address in the pointer. |
| Address |
Use the & operator.
|
| Value |
Use the * operator with the pointer.
|
int *p mean?char *p?Watch a beginner-friendly explanation of pointer declaration and initialization in C.
A short handwritten-style revision sheet for pointer declarations will be provided here.
Use the mind map for pointer declaration, initialization, address and indirection.