Array and pointer have a close relationship but both are different concepts in C programming. In this blog post, I will discuss the difference between pointer to an array and array of pointers.
Pointer to an Array:
A pointer is a very important concept of C language. We can create a pointer to store the address of an array. This created pointer is called a pointer to an array. A pointer to an array is useful when we need to pass a multidimensional array into a function.
Pointer to an array is also known as an array pointer. We are using the pointer to array to access the elements of the array. It is important to know how to create a pointer to an array when working on a multi-dimension array.
Declaration of a pointer to a 1D Array:
data_type (*var_name)[size_of_array];
Example,
int (*ptr)[5];
Here ptr is a pointer that can point to an array of 5 integers. Since subscript has higher precedence than indirection, it is necessary to enclose the indirection operator and pointer name inside parentheses. Here the type of ptr is ‘pointer to an array of 5 integers’.
So let see a C program to understand how we can create a pointer to an array and how we can use it in our program.
#include<stdio.h> #define ARRAY_SIZE 5 int main() { int arr[ARRAY_SIZE] = {1,2,3,4,5}; int i = 0; // Pointer to an array of integers int (*ptr)[ARRAY_SIZE]; // Points to the whole array arr. ptr = &arr; for(i=0; i< ARRAY_SIZE ; ++i) { printf(" arr[%d] = %d\n",i,(*ptr)[i]); } return 0; }
Output:
Array of pointers:
As we know an array is essentially a collection of elements of the same data types. All elements must be the same and store at the contiguous memory location.
So we can create an array of pointers, it is basically an array of the pointer variables. It is also known as pointer arrays.
Declaration of an array of pointers:
data_type *arrar_name[array_size];
Example,
int *arr[5];
Here “arr” is an array of 5 integer pointers.
So let see a C program to understand how we can create an array pointer and how we can use it in our C program.
#include <stdio.h> int main() { int a = 10; int b = 20; int c = 30; int i = 0; // Creating an array of integer pointers // and initializing it with integer variables address int *arr[3] = {&a,&b,&c}; // printing values using pointer for (i = 0; i < 3; ++i) { printf("Value of arr[%d] = %d\n", i, *arr[i]); } return 0; }
Output:
I hope now you able to differentiate pointer to an array and array of pointers. So let see a program to understand the pointer to an array. I request here try to solve this program.
#include<stdio.h> #define ARRAY_SIZE 5 int main() { int arr[ARRAY_SIZE] = {1,2,3,4,5}; int *p; //Pointer to an array of integers int (*ptr)[ARRAY_SIZE]; //Points to the whole array arr. ptr = &arr; //Assign Array to pointer.. p = arr; printf("p = 0x%p, ptr = 0x%p\n", p, ptr); //increment pointer to an array and integer pointer p++; ptr++; printf("p = 0x%p, ptr = 0x%p\n", p, ptr); return 0; }
Code explanation:
In the above program, I have created a pointer to an array of 5 integers ( ptr ) and integer pointer ( p ). I am assigning the address of the array and address of the first element to the pointers.
Now in the last, I am displaying the stored address by the pointers before and after performing increment operation. So the output will be,
p = 0x0061FF04, ptr = 0x0061FF04 p = 0x0061FF08, ptr = 0x0061FF18
Because the base type of p is int while the base type of ptr is ‘an array of 5 integers’. We know that the pointer arithmetic is performed relative to the base size, so if we write ptr++, then the pointer ptr will be shifted forward by 20 bytes.
You can see this Article, for more details,
What is the difference between array_name and &array_name?
To understand this question let’s take an example, suppose arr is an integer array of 5 elements.
int arr[5];
If you print arr and &arr then you found the same result but both have different types.
arr=> The name of the array is a pointer to its first element. So here arr split as the pointer to the integer.
&arr=> It split into the pointer to an array that means &arrwill be similar to int(*)[5];
#include<stdio.h> int main() { int arr[5] = {0}; printf("arr= %u\n\n", arr); printf("&arr= %u\n\n", &arr); printf("arr+1 = %u\n\n", arr+1); printf("&arr+1 = %u\n\n", &arr+1); return 0; }
When you compile the above code, you will find arr and &arris same but the output of arr+1 and &arr+1 will be not the same due to the different pointer type.
Recommended Articles for you:
- Difference between pointer and array in C?
- How to pass an array as a parameter in C?
- How to create dynamic array in C?
- 15 Common mistakes with memory allocation.
- Introduction of Array in C.
- What is flexible array member in c?
- What is importance of struct hack in c?
- How to access a two-dimensional array using pointers in C?
- Arithmetic operation on pointer in C.
- A brief description of the pointer in C.
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Memory Layout in C.
- 100 embedded C interview Questions.
- Python Interview Questions with Answer.
- File handling in C.
- Function pointer in structure.
- void pointer in C, A detail discussion.
- 100 c interview questions, your interviewer might ask.
- File handling in C.
- C format specifiers.