Array of pointers

3
142

We can have the array of pointers which is generally very useful in handling strings of varying length in a single array. Suppose we have to save 5 names in an array (Ram, Laxman, Sita, Dashavatar, Hanuman), then to save them we need a two-dimensional array of 5 rows and 10 columns at least because the 2-D array cannot be defined with different columns. Clearly, some columns are wasted for some names as they are not of that much size. Whereas with the help of array of pointers we can use as much memory needed as shown below: –

// Creating Five different Strings in memory.
  char str1[]= "Ram";
  char str2[]= "Sita";
  char str3[]= "Laxman";
  char str4[]= "Dashavatar";
  char str5[]= "Hanuman";
  char *pointer_name[5];    // An array of pointers to hold char addresses
  // Assigning the addresses of strings to five pointers
  pointer_name[0] = str1;    
  pointer_name[1] = str2;
  pointer_name[2] = str3;
  pointer_name[3] = str4;
  pointer_name[4] = str5;

Now see same array without using pointer

char array_name[5][15] = {"Ram", "Sita", "Laxman", "Dashavatar", "Hanuman"};

Now in memory they will be represented as below: –

In the first case, five names will be stored at five locations occupying (3+4+6+10+7 = 30) bytes for strings and 5 bytes for storing 5 pointers. In the second case, a consecutive space of (5×15= 75) bytes is required in memory. As the names increase, this gap will increase resulting in memory wastage in the second case. Following program will use them: –

#include<stdio.h>
int main() 
{
  int i=0,j=0;
  // Creating Five different Strings in memory.
  char str1[]= "Ram";
  char str2[]= "Sita";
  char str3[]= "Laxman";
  char str4[]= "Dashavatar";
  char str5[]= "Hanuman";
  // An array of pointers to hold char addresses
  char *pointer_name[5];	
  // Assigning the addresses of strings to five pointers
  pointer_name[0] = str1;	
  pointer_name[1] = str2;
  pointer_name[2] = str3;
  pointer_name[3] = str4;
  pointer_name[4] = str5;
  for(i=0; i<5; i++) 
  printf("%s\n",pointer_name[i]);
  char array_name[5][15] = {"Ram", "Sita", "Laxman", "Dashavatar", "Hanuman"};
  for(i=0; i<5; i++)
  {
    for(j=0; j<15; j++)
    {
      printf("%c",array_name[i][j]);
    }
    printf("\n");
  }
  return 0;
}
Ram
Sita
Laxman
Dashavatar
Hanuman
Ram
Sita
Laxman
Dashavatar
Hanuman

3 COMMENTS

  1. Inquiry from New York

    Hello,

    I am Mondy from Thelocation3, we work with coverwallet to protect the cause of entrepreneurs.

    We will analyze your needs online, and provide a customized insurance quote that fits your budget.

    The right insurance will give you the peace of mind to focus on what matters – running your business

    If you are interested, take 5 minutes to explore and we are ready to serve you: https://www.thelocation3.com/getquote

    Look forward to hear from you,

    Rozanne Mondy

    Company: Thelocation3
    Address: 3961 Victory Blvd, Staten Island, NY 10314
    Unsubscribe: https://www.thelocation3.com/unsubscribe

  2. Invite you to review our services

    Hello there,

    I’m is Loftin from Biglep.
    Biglep cooperates with Automattic, Google adsense, etc., provides them with data that webmasters’ opinions on their product.

    I am contacting you because we are looking for webmaster/blogger/business owner like you to test our service.

    The companies spend billions of dollars each year to obtain survey data.
    Biglep pays each member *** dollars daily for their opinions.

    If you are interested, please visit: https://www.biglep.com/paidsurvey

    Thank you for your time,

    Jennifer Loftin

    ^^^^^^^^^^^^
    Unsubscribe: https://www.biglep.com/unsubscribe

LEAVE A REPLY

Please enter your comment!
Please enter your name here