Array of Pointers

CCBeginner
Practice Now

Introduction

In C programming, an array of pointers is an array in which each element is a pointer to another data type, such as int, char, float, etc. In this lab, we will learn how to access an array of integer pointers and an array of character pointers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/constants("`Constants`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/CompoundTypesGroup -.-> c/strings("`Strings`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123201{{"`Array of Pointers`"}} c/comments -.-> lab-123201{{"`Array of Pointers`"}} c/variables -.-> lab-123201{{"`Array of Pointers`"}} c/data_types -.-> lab-123201{{"`Array of Pointers`"}} c/constants -.-> lab-123201{{"`Array of Pointers`"}} c/operators -.-> lab-123201{{"`Array of Pointers`"}} c/for_loop -.-> lab-123201{{"`Array of Pointers`"}} c/arrays -.-> lab-123201{{"`Array of Pointers`"}} c/strings -.-> lab-123201{{"`Array of Pointers`"}} c/memory_address -.-> lab-123201{{"`Array of Pointers`"}} c/pointers -.-> lab-123201{{"`Array of Pointers`"}} c/function_parameters -.-> lab-123201{{"`Array of Pointers`"}} c/function_declaration -.-> lab-123201{{"`Array of Pointers`"}} end

Array of Integer Pointers

In this step, we will access an array of integer pointers. Open the main.c file in the ~/project/ directory and paste the following code:

#include <stdio.h>

// Global declaration.
const int MAX = 5;

int main() {
  printf("\n\n\t\t==== Array of Integer Pointers ====\n\n\n");

  int var[] = {10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers
  int i = 0;

  /*
      ptr is an array of int pointers i.e.
      it stores the address of each array element
  */
  int *ptr[MAX];

  for(i = 0; i < MAX; i++) {
    /*
        Assign the address of each of the array
        element to the ptr array
    */
    ptr[i] = &var[i];
  }

  for(i = 0; i < MAX; i++) {
    /*
        ptr[i] stores the address of the element var[i].
        Hence, *ptr[i] returns the value of the element
        stored at location ptr[i]
    */
    printf("Value of var[%d] = %i\n\n", i, *ptr[i]);
  }

  printf("\n\n\t\t==== End of Program ====\n\n\n");
  return 0;
}

This program initializes an array of integer pointers and assigns the address of each array element to the pointer array. Then, it prints the value of each element using the pointer array.

Array of Character Pointers

In this step, we will access an array of character pointers. Paste the following code after the previous step's code:

#include <stdio.h>

// Global declaration.
const int MAX = 4;

int main() {
  printf("\n\n\t\t==== Array of Character Pointers ====\n\n\n");

  char *names[] = {"Google", "Amazon", "Facebook", "Apple"}; // initializing an array(here names) of char pointers
  int i = 0;

  for(i = 0; i < MAX; i++) {
    printf("Value of names[%d] = %s\n\n", i, names[i]);
  }

  printf("\n\n\t\t==== End of Program ====\n\n\n");
  return 0;
}

This program initializes an array of character pointers and assigns the address of the first character of each string to the pointer array. Then, it prints the complete name of each element using the pointer array.

Compile and Run the Program

In this step, we will compile and run the program. Open the terminal and navigate to the ~/project/ directory. Type the following command to compile the program:

gcc main.c -o main

After successful compilation, type the following command to run the program:

./main

This command will execute the program and show the output on the terminal:

		==== Array of Integer Pointers ====


Value of var[0] = 10

Value of var[1] = 20

Value of var[2] = 30

Value of var[3] = 40

Value of var[4] = 50


		==== Array of Character Pointers ====


Value of names[0] = Google

Value of names[1] = Amazon

Value of names[2] = Facebook

Value of names[3] = Apple


		==== End of Program ====

Full Code

#include <stdio.h>

// Global declaration.

const int MAX = 5;

int main() {
  printf("\n\n\t\t==== Array of Integer Pointers ====\n\n\n");

  int var[] = {10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers
  int i = 0;

  /*
      ptr is an array of int pointers i.e.
      it stores the address of each array element
  */
  int *ptr[MAX];

  for(i = 0; i < MAX; i++) {
    /*
        Assign the address of each of the array
        element to the ptr array
    */
    ptr[i] = &var[i];
  }

  for(i = 0; i < MAX; i++) {
    /*
        ptr[i] stores the address of the element var[i].
        Hence, *ptr[i] returns the value of the element
        stored at location ptr[i]
    */
    printf("Value of var[%d] = %i\n\n", i, *ptr[i]);
  }

  printf("\n\n\t\t==== Array of Character Pointers ====\n\n\n");

  char *names[] = {"Google", "Amazon", "Facebook", "Apple"}; // initializing an array(here names) of char pointers
  i = 0;

  for(i = 0; i < MAX; i++) {
    printf("Value of names[%d] = %s\n\n", i, names[i]);
  }

  printf("\n\n\t\t==== End of Program ====\n\n\n");
  return 0;
}

Summary

In this lab, we have learned how to access an array of integer pointers and an array of character pointers. An array of pointers is an array in which each element is a pointer to another data type. We have learned how to initialize an array of pointers, how to assign the addresses of array elements to a pointer array, and how to access the array elements using the pointer array.

Other C Tutorials you may like