C Tutorial – Pointers

C Array of Pointers

Array of Pointers are very useful in C, especially when you deal with Strings.

Let’s review a small example.

We start declaring a array of pointer as follow:

char *mylist[] = {

We also want to declare a variable type integer that we will use later.

int yourselection;

In our example we will give the user the option to select the programming language that is like the most.

 printf("What do you like programming\n");


    printf("Please enter your selection:");

Now we ask the user to select one option:

printf("Please enter your selection:");

In the code above we read the option provided by the user and then we print it out as shown in the figure below:

If you notice we call the array of pointer “mylist” and we pass the parameter that the user has selected.

Using array of pointers has also one benefit especially when dealing with strings: we can compare easily between 2 strings without need to check character by character the string nor we need to use the library function strcmp

Let’s view a practical application of that. Once the user selected is option the program will check if the user like C the most or not and print a different statement.

Here is the code:

 if (mylist[yourselection]==mylist[1]) {
        printf("You selected C, Congratulation\n");

          printf("You have selected %s", mylist[yourselection] );  
          printf(" which is not my favorite\n");

Here is what happen if we select Java for example

As you can see we were able to compare strings very easily without going character by character.

Here is the full program

Leave a Reply

Your email address will not be published. Required fields are marked *