Array of Pointers in C with Examples

This C Tutorial Explains an Array of Pointers in C with Examples.

What is an array?
Array is a variable that can store multiple values ​​in a single variable rather than having separate variables for each element.

What is a Pointer?
Pointer is a variable, which stores the address of another variable. It stores the direct address of the memory location. The asterisk * symbol (unary operator) is used to declare a pointer. The pointer should be of the same data type, as of the variable, whose address it will store.

What is Array of Pointers in C?
An array of pointers is a data structure that stores a collection of pointers. Pointers are variables that store the address of another variable. Each element in the array is a pointer to another variable. The array of pointers can be used to store a collection of data, such as an array of strings, integers, or even other arrays. To access the data stored in the array of pointers, you need to dereference the pointer.

How can I declare and initialize an array of pointers?

Well! We have situations where we need several pointers, say several strings to be matched for desired one, for example, then declaring and initializing pointers one by one as scalar is a cumbersome job. We have a way out to this trouble and which is declare and initialize an array of pointers. Let’s go for it.

    char *car_make[9] = {"Suzuki","Toyota","Nissan","Tata","BMW","Audi", 
                         "Chevrolet","Honda","Mahindra"};

Let’s presume the declaration “char *car_make[9]” as an expression and evaluate it to understand about how to declare and initialize correctly an array of pointers.

advertisement
advertisement

In the expression

    char *car_make[9]

subscript operator ‘[]’ being higher in precedence than indirection operator ‘*’ and therefore is evaluated first, results in 9 characters. Then, indirection is applied which results in 9 ‘pointers-to-char’ or 9 ‘pointers-to-strings’. Let’s rewrite the above expression

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
    char *car_make[9]

as

    char (*car_make)[9]

and this time, again, try to evaluate the expression.

This time, indirection operator ‘*’, enclosed in parenthesis, go first than subscript operator. This results in ‘pointer-to-something’. Then subscript operator evaluates to an ‘array of 9 characters’. And overall result is a ‘pointer-to-array_of_9_characters’, which is not what we are interested in. In fact, we require an array of pointers. Therefore, expression

advertisement
    char *car_make[9]

is what serves our purpose. Let’s see how this initialization takes place,

    char *car_make[9] = {"Suzuki","Toyota","Nissan","Tata","BMW","Audi",
                         "Chevrolet","Honda","Mahindra"};
    *car_make[0] = "Suzuki";    /* '*car_make[]' is a ptr-to-string */
    *car_make[1] = "Toyota";
    *car_make[2] = "Nissan";
    *car_make[3] = "Tata";
    *car_make[4] = "BMW";
    *car_make[5] = "Audi";
    *car_make[6] = "Chevrolet";
    *car_make[7] = "Honda";
    *car_make[8] = "Mahindra";

Let’s consider a program using the above array of ‘pointer-to-strigs’ to ensure if a given string is in the array or not.

advertisement
/*
 * arr_ptr2char.c -- program shows if a given string is matched in the list
 * of strings
 */
#include <stdio.h>
#include <string.h>
 
/* function prototype */
void match_str(char const * const, char const * const[], int const);
 
int main(void)
{
    char const *car_make[9] = {"Suzuki","Toyota","Nissan","Tata","BMW", 
                               "Audi","Chevrolet","Honda","Mahindra"};
    char to_search[15];
 
    printf("Give choice of \'car make\' u want to search for...\n");
    gets(to_search);
    match_str(to_search, car_make, 9);
    return 0;
}
 
void match_str(char const * const to_match, char const * const arr_str[],
                int const size)
{
    char const **check_make;
 
    for (check_make = arr_str; check_make < arr_str + size; check_make++)
        if (strcmp(to_match, *check_make) == 0) {
            printf("Matched at location %d in car_make.\n",
                    check_make - arr_str);
            return;
         }
 
    printf("Not matched!\n");
    return;
}

Let’s now try out above program for some test-choices,

Give choice of 'car make' u want to search for...
Nissan
Matched at location 2 in car_make.
 
Give choice of 'car make' u want to search for...
Tata
Matched at location 3 in car_make.
 
Give choice of 'car make' u want to search for...
Ambassador
Not matched!

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

advertisement
advertisement
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 & discussions at Telegram SanfoundryClasses.