Pointer to an Array

What is pointer to an array? Explain with example.

Questions by kakarukoireng

Showing Answers 1 - 3 of 3 Answers

rayvarshney

  • Aug 28th, 2010
 

A multi dimensional array can be expressed in terms of an array of pointers rather than as a pointer to a group of contiguous array.

ex:suppose that 'x' is a two-D integer array having 10 rows and 20 columns,we can define 'x' as a 1-D array of pointers by writing
int *x[10];

  Was this answer useful?  Yes

Actually there does not exist array rather here is a term called pointer to an array.

let me explain with the help of one example.suppose we have following code
#include<stdio.h>
#define i 10
main()
{
int a[i];
.
.
.
.}
here we have array called int a[i] which means that it is the pointer of an array and also be represented as *(a+i).
it is the main reason that an array is initiallised with 0 as pointer always initialise with 0.a[i] can also be written as i[a] and *(i+a)

For any given data type T, a pointer to an array of T is declared as "T (*arr)[N];".  It has the same value as a pointer to the first element of the array, but a different type (T (*)[N] as opposed to T *). 

A pointer to an array can be obtained by using the "&" operator on an array expression, as in

int arr[10];
int (*parr)[10] = &arr;

A multi-dimensional array expression will also "decay" into a pointer to array type; given the declaration "int arr[5][6];", the type of the expression "arr" will be "int (*)[6]" (pointer to a 6-element array of int) except if "arr" is an operand of either the sizeof or address-of operators. 

When you are using a pointer to an array, you must dereference the pointer expression before applying the subscript, as in "x = (*parr)[i];".

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions