in Technology by
What is the difference between pointer to an array and array of pointers?

1 Answer

0 votes
by

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:

pointer to an array in C

 

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:

array of Pointers

Related questions

0 votes
    I wrote a C# function of DLL that returns a byte pointer that points to an array of pointers that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 3, 2022 in Education by JackTerrance
0 votes
    In a _______ clustering index, the index record contains the search-key value and a pointer to the first ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What are the advantages of using an array of pointers to string instead of an array of strings?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is an array of pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the difference between an uninitialized pointer and a null pointer?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the difference between near, far and huge pointers?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is difference between far and near pointers?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I have Conversation classes with a members relation attribute pointing to User class. This members attribute consists ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    What is the difference between the length of an Array and size of ArrayList in Java?...
asked May 27, 2021 in Technology by JackTerrance
0 votes
    Can a pointer access the array?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    Can a pointer access the array in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    What is the difference between Array and ArrayList in JAVA?...
asked May 27, 2021 by JackTerrance
0 votes
    Differentiate between a constant pointer and pointer to a constant?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    An ____________ consists of a search-key value and pointers to one or more records with that value as ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    The ___ algorithm scans each index for pointers to tuples that satisfy an individual condition. (a) A2 (b ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...