Skip to main content

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 = &num; // 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 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; // Declare and initialize an integer variable num
int *ptr = &num; // 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 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 DeclarationPointer Definition
Only specifies the type of the pointer variable and its nameAssigns a memory address to the pointer variable, making it point to a specific variable
Example: int *ptr;Example: int *ptr = &num;
Declares a pointer without initializing itDeclares and initializes a pointer in the same statement
Memory is not allocated for the pointer variableMemory is allocated for the pointer variable
Can be used to declare multiple pointers of the same type in a single statementCannot declare multiple pointers of the same type and initialize them in a single statement