Pointer to an Array in C

This C Tutorial Explains Pointer to Array in C with Example(s).

A pointer is a variable whose contents is an address of some location in memory. For example:

    int val = 10, new = 20;
    int *ptr = &val;        /* 'ptr' points to 'val' */
    ptr = &new;             /* 'ptr' now points to 'new' */

Why do we need a pointer to an array? I recall, We have already seen that when we pass an array as a function argument, we use pointer as function parameter to receive the array, just start of array, and use this pointer to perform manipulations on the original values, if required, without copying the array into function. This saves critical resources time and memory. So, what about if we want a pointer to an array, let’s say one-dimensional array, for example:

    int clubs[5] = {23, 55, 12};

As we know that name of an array is a pointer constant i.e. a constant address in memory and pointer contains an address. Therefore, we can declare a pointer to an array. But what type is it? For one-dimensional array ‘clubs[]’ which is an array of integers, type of array is pointer-to-int, so we write as:

    int *ptr = clubs;          /* 'clubs' is a constant address */

Now ‘ptr’ and ‘clubs’ have same contents i.e. same address. Therefore, ‘ptr’ points to array ‘clubs’. In fact, it points to first element ‘clubs[0]’ of array ‘clubs’. In order to have ‘ptr’ to point to next element, ‘clubs[1]’, in the array, we increment the ‘ptr’ by 1 as,

    ptr++;                     /* 'ptr' is incremented by 1 */

and so on. Now, let’s consider a two-dimensional array and try declaring a ‘ptr’ to it.

    int hawks[5][3];	       /* a two dimensional array */

As we know that in order to declare a pointer to an array, we must know the type of array. What type of array ‘hawks’ is? As we see ‘hawks’ is an array of 5 arrays of 3 integers each. Deciphering type of array from its expression is a key point in declaring a pointer-to-array.

So, what type ‘hawks’ is? From the expression, it’s clear that ‘hawks’ is an array of arrays of 3 integers. So, ‘hawks’ contains elements as array of 3 ints. Therefore, type of ‘hawks’ is: array of array of 3 ints, so is type of pointer,

    int (*ptr)[3] = hawks;	/* 'ptr' points to 'hawks' */

Notice that we enclosed ‘ptr’ in a pair of parenthesis because subscript operator ‘[]’ have higher precedence than indirection operator (*). so, ‘ptr’ is a pointer-to-array-of_3ints, i.e. ‘ptr’ points to first element of ‘hawks’ which is an array of 3 integers. In fact, ‘ptr’ points to hawks[0], more precisely to hawks[0][0].

What happens if ‘ptr’ gets incremented by 1? That is,

    ptr += 1;	                /* ? */

‘ptr’, since, points to an array of 3 integers, when gets incremented by 1,

    ptr + 1;

1 is scaled by the size of element ‘ptr’ is pointing to, i.e. size of array of 3 integers. Therefore, ‘ptr’ points to next array of 3 integers which is ‘hawks[1][3]’ or more precisely ‘hawks[1][0]’ and so on.

Similarly, we can declare pointer to multi-dimensional arrays of higher dimensions. For example, pointer to 4-dimensional array,

    int overs[5][5][5][5];	  /* a four-dimensional array */

can be declared as

    int (*ptr4)[5][5][5];
    /* 'ptr' points to array of 5 arrays of 5 arrays of 5 ints */

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.