Defining Pointers in C
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:
datatype *ptr_name;
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; // Declare and initialize an integer variable num
int *ptr; // Declare an integer pointer variable ptr
ptr = # // Assign the memory address of num to ptr
printf("Value of num: %d\n", num); // Print the value of num
printf("Address of num: %p\n", &num); // Print the address of num
printf("Value of ptr: %p\n", ptr); // Print the value of ptr
return 0;
}
Output:
Explanation
- We declare an integer variable
num
and initialize it with the value 10. We also declare an integer pointer variableptr
without initializing it. - Next, we assign the memory address of the
num
variable to theptr
variable using the&
operator. Now, theptr
variable points to the memory location wherenum
is stored. - Then, we then use
printf()
statements to print the value ofnum
, the address ofnum
, and the value ofptr
. The%d
format specifier is used to print the value ofnum
as an integer, and the%p
format specifier is used to print the address ofnum
and the value ofptr
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; // Declare and initialize an integer variable num
int *ptr = # // Define a pointer variable ptr and assign the address of
// num to it
printf("Value of num: %d\n", num); // Print the value of num
printf("Address of num: %p\n", &num); // Print the address of num
printf("Value of ptr: %p\n", ptr); // Print the value of ptr
return 0;
}
Output:
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 thenum
variable to it in the same statement, using the&
operator. - We then use
printf()
statements to print the value ofnum
, the address ofnum
, and the value ofptr
. - The
%d
format specifier is used to print the value ofnum
as an integer, and the%p
format specifier is used to print the address ofnum
and the value ofptr
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 |