Difference between int *a and int **a in C
In C, the declarations int *a and int **a represent two different concepts related to pointers. Pointers play a fundamental role in memory management and data manipulation in C programming so it is important to have a clear understanding of them.
What does int * means?
This declares a pointer to an integer. It means that the variable a can store the memory address of the integer variable.
You can use another variable of type integer pointer ‘a’ to point to a single integer variable:
int x = 10;
int *a = &x; // ‘a’ points to the integer variable ‘x’
a stores the address of an integer variable, so you can dereference it using the * operator to access the value of the integer it points to like *a.
Example
C
// C Program to illustrate 'int *' #include <stdio.h> int main() { int x = 20; int * a = &x; // 'a' is a pointer to an integer // pointing to 'x' printf ( "The Value of x: %d\n" , x); printf ( "The Value pointed to by 'a': %d\n" , *a); return 0; } |
The Value of x: 20 The Value pointed to by 'a': 20
In this example, int *a; declares a single-level pointer a which points to the integer variable x. a stores the memory address of the x and you can access the value of x using the *a.
Understanding ‘int **’
This declares a pointer to a pointer to an integer. It means that the variable a can store the memory address of the pointer to an integer variable.
You can use a to point to a pointer to an integer:
int x = 10;
int *p = &x; // ‘p’ is a pointer to an integer, pointing to ‘x’
int **a = &p; // ‘a’ is a pointer to a pointer to an integer, pointing to ‘p’
In this case, a points to a pointer p that in turn points to an integer x. You can use a to access the address of p and then use *a to access the value of p which is the address of x.
Example
C
// C Program to illustrate 'int **' #include <stdio.h> int main() { int x = 20; int * p = &x; int ** a = &p; printf ( "x: %d\n" , x); printf ( "p: %d\n" , p); printf ( "*p: %d\n" , *p); printf ( "a: %d\n" , a); printf ( "*a: %d\n" , *a); printf ( "**a: %d\n" , **a); return 0; } |
x: 20 p: -1569282476 *p: 20 a: -1569282472 *a: -1569282476 **a: 20
In this example, int **a; declares a double-level pointer a which points to a pointer to an integer p. The p itself points to the integer variable x. You can access the value of x indirectly through a by first dereferencing a to get the address of the p and then dereferencing p to access the value of x.
Difference Between ‘int *’ and ‘int **’ in C
Properties |
int *a; |
int **a; |
---|---|---|
Pointer Type | Points to an integer variable. | Points to a pointer to an integer. |
Usage | Directly points to a value. | Points to a pointer that points to value. |
Declaration | int *a; | int **a; |
Initialization | int *a = &x; where x is an integer variable. | int **a = p; where p is a pointer to an integer. |
Dereferencing | Access the value using *a. | Access the value using the **a. |
Typical Use Cases | Commonly used for single values, arrays, and dynamic memory allocation. | Used when working with the multi-dimensional arrays, dynamic memory allocation, or functions that modify pointers. |
Example | int x = 10; int *a = x; | int x = 10; int *px = x; int **a = px; |
Contact Us