Difference between int *a and int **a in C
Last Updated : 12 Oct, 2023
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; }
OutputThe 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; }
Outputx: 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; |
---|
Similar Reads
Difference between int* p() and int (*p)()? A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type
2 min read
Difference between Array and Union in C 1. Array in C : An array is collection of similar data items accessed by a common name stored at continuous memory locations. The elements of an array can be accessed using indices. They can be used to store primitive data types such as int, float, double, char, etc. but all elements have to be of t
2 min read
Difference between "int main()" and "int main(void)" in C/C++? Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C int main() { /* */ return 0; } C++ int main() { /* */ return 0; } Definition 2: C int main(vo
3 min read
What is the difference between "char a" and "char a[1]"? Question Source: Aricent Interview Although both expressions can be used to create a variable to store one character, there are following differences. 1) "char a" represents a character variable and "char a[1]" represents a char array of size 1. 2) If we print value of char a, we get ASCII value of
1 min read
Difference between ++*p, *p++ and *++p Predict the output of following C programs. C // PROGRAM 1 #include <stdio.h> int main(void) { int arr[] = {10, 20}; int *p = arr; ++*p; printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p); return 0; } C // PROGRAM 2 #include <stdio.h> int main(void) { int arr[] = {
2 min read
Difference between Keyword and Identifier in C In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff
3 min read