Pointer Declaration
Pointer declaration is the process of creating a pointer variable that can hold the memory address of a variable or a memory location.
The syntax for pointer declaration in C is as follows:
Here, datatype
is the data type of the variable or memory location that the pointer will point to, and ptr_name
is the name of the pointer variable. The *
symbol indicates that the variable is a pointer.
Pointer Declaration - Example
#include <stdio.h>
int main()
{
int num = 10;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
return 0;
}
Explanation
- We declare an integer variable
num
and initialize it with the value 10. We also declare an integer pointer variable ptr
without initializing it. - Next, we assign the memory address of the
num
variable to the ptr
variable using the &
operator. Now, the ptr
variable points to the memory location where num
is stored. - Then, we then use
printf()
statements to print the value of num
, the address of num
, and the value of ptr
. The %d
format specifier is used to print the value of num
as an integer, and the %p
format specifier is used to print the address of num
and the value of ptr
as pointers.
Pointer Definition
A pointer definition is the process of assigning a value to a pointer variable, which is the memory address of a variable or memory location.
The pointer variable stores the address of the variable or memory location, rather than the value of the variable or memory location itself.
The syntax for pointer definition in C is as follows:
datatype *ptr_name = &var_name;
Here, datatype
is the data type of the variable or memory location that the pointer will point to, ptr_name
is the name of the pointer variable, and var_name
is the name of the variable or memory location whose address will be assigned to the pointer.
Pointer Definition - Example
#include <stdio.h>
int main()
{
int num = 10;
int *ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
return 0;
}
Explanation
- We declare an integer variable
num
and initialize it with the value 10. - We also define an integer pointer variable
ptr
and assign the memory address of the num
variable to it in the same statement, using the &
operator. - We then use
printf()
statements to print the value of num
, the address of num
, and the value of ptr
. - The
%d
format specifier is used to print the value of num
as an integer, and the %p
format specifier is used to print the address of num
and the value of ptr
as pointers.
Declaration vs Definition
Here is a table that highlights the differences between pointer declaration and definition in C
Pointer Declaration | Pointer Definition |
---|
Only specifies the type of the pointer variable and its name | Assigns a memory address to the pointer variable, making it point to a specific variable |
Example: int *ptr; | Example: int *ptr = # |
Declares a pointer without initializing it | Declares and initializes a pointer in the same statement |
Memory is not allocated for the pointer variable | Memory is allocated for the pointer variable |
Can be used to declare multiple pointers of the same type in a single statement | Cannot declare multiple pointers of the same type and initialize them in a single statement |