DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Pointers , Arrays & Strings in C
MissMati
MissMati

Posted on

Pointers , Arrays & Strings in C

INTRODUCTION

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.

#include <stdio.h>

int main () {

   int  variable1;
   char variable2[10];

   printf("Address of var1 variable: %x\n", &variable1);
   printf("Address of var2 variable: %x\n", &variable2);

   return 0;
}

Enter fullscreen mode Exit fullscreen mode
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
Enter fullscreen mode Exit fullscreen mode

A String is a data type that stores the sequence of characters in an array. A string in C always ends with a null character (\0), which indicates the termination of the string


char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};
Enter fullscreen mode Exit fullscreen mode

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc.

#include<stdio.h>  
int main(){      
int i=0;    
int marks[5]={20,30,40,50,60};//declaration and initialization of array    
 //traversal of array    
for(i=0;i<5;i++){      
printf("%d \n",marks[i]);    
}    
return 0;  
}    
Enter fullscreen mode Exit fullscreen mode
20
30
40
50
60
Enter fullscreen mode Exit fullscreen mode

What is an Array of Pointers to String

Pointers contain addresses of the particular variable that we need. An array of pointers stores the addresses of all the elements of the array and an array of string pointers stores the addresses of the strings present in the array. The array contains the base address of every String element in the array.

Here is an example to illustrate this:

char *arr[]={
            "Big",
            "black ",
            "Heavy",
            "Nice",
            "ball"
          };
Enter fullscreen mode Exit fullscreen mode

String array using the 2D array:
As we know the array is a collection of similar data types and all the data stored in the contiguous memory location. So, in this case, each character will be placed at contiguous memory locations. Example

char arr[ROW][COL]; //2d array of character
Enter fullscreen mode Exit fullscreen mode

2DArray

This is to mean if we create a 2D array then we have to create an array with a column count at least equal to the longest String and it leads to a lot of space wastage in the array elements with a smaller value.

String array using the array of pointer to string:

Similar to the 2D array we can create the string array using the array of pointers to strings. Basically, this array is an array of character pointers where each pointer points to the string’s first character.
syntax

char *arr[ROW]; //array of pointer to string
Enter fullscreen mode Exit fullscreen mode


c

array of pointer to string:

Based on how you want to represent the array of strings, you can define a pointer to access the string from the array. let see a few example code,

**

1.) Access 2d array of characters using the pointer to the array

**
To access the string array, we need to create a pointer to the array and initialize the pointer with the array. Now using the for loop you can read all the strings of the array.

Pointer to the 1D array:

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char * arr[5] = {"Cloud1", "Cloud1Cloud1", "Cloud1Cloud1Cloud1", "Cloud1Cloud1Cloud1Cloud1", "Cloud1Cloud1Cloud1Cloud1"};
    //create pointer to the array
    char * (*ptrArr)[5] = NULL;
    //initialize the pointer
    ptrArr = &arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("%s \n", (*ptrArr)[row]);
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

/tmp/wY4tusyiIu.o
Cloud1 
Cloud1Cloud1 
Cloud1Cloud1Cloud1 
Cloud1Cloud1Cloud1Cloud1 
Cloud1Cloud1Cloud1Cloud1
Enter fullscreen mode Exit fullscreen mode

Pointer to the 2D array

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char arr[5][10] = {"Coding", "Coding", "Coding", "Coding", "Coding"};
    //create pointer to the 2d array
    char (*ptrArr)[5][10] = NULL;
    //initialize the pointer
    ptrArr = &arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("%s \n", (*ptrArr)[row]);
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

/tmp/wY4tusyiIu.o
Coding 
Coding 
Coding 
Coding 
Coding 
Enter fullscreen mode Exit fullscreen mode

** pointer to pointer**

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char * arr[5] = {"pointer2pointer", "pointer2pointer", "pointer2pointer", "pointer2pointer", "pointer2pointer"};
    //create pointer to the array
    char **ptr = NULL;
    //initialize the pointer with array
    ptr = arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("   %s \n", ptr[row]);
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

/tmp/wY4tusyiIu.o
pointer2pointer 
   pointer2pointer 
   pointer2pointer 
   pointer2pointer 
   pointer2pointer

Enter fullscreen mode Exit fullscreen mode

Advantages of String pointer Array

  • It occupies less space in the memory: Compared to a string array, an array of pointers to string occupies less space. This means effective use of memory space because if we create a 2D array then we have to create an array with a column count at least equal to the longest String and it leads to a lot of space wastage in the array elements with a smaller value.

  • Manipulation of strings: An array of pointers to string allows greater ease in manipulating strings and performing different operations on strings.

Top comments (0)

Let's team up together 🀝

We're Hiring

We're hiring for a Senior Full Stack Engineer to join the DEV team. Want the deets? Head here to learn more about who we're looking for.