Pointers , Arrays & Strings in C
Tags: c, programming, pointers
Published: 2022-10-11 04:08:27

## 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.

```c
#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;
}

```
```
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
```

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
```c

char c[] = "abcd";

char c[50] = "abcd";

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

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


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. 

```c
#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;  
}    
```
```
20
30
40
50
60
```

**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:

```c
char *arr[]={
            "Big",
            "black ",
            "Heavy",
            "Nice",
            "ball"
          };
```

_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

```c
char arr[ROW][COL]; //2d array of character
```
![2DArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wim7m7o46o32ljltd00.png)

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 
```c
char *arr[ROW]; //array of pointer to string
```c 

![array of pointer to string:](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lplrzmngzinixjb8jrz.png)

 
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:**
 
```c
#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;
}
```
Output:
```
/tmp/wY4tusyiIu.o
Cloud1 
Cloud1Cloud1 
Cloud1Cloud1Cloud1 
Cloud1Cloud1Cloud1Cloud1 
Cloud1Cloud1Cloud1Cloud1
```

**Pointer to the 2D array**
 
```c
#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;
}
```

Output:

```
/tmp/wY4tusyiIu.o
Coding 
Coding 
Coding 
Coding 
Coding 
```
** pointer  to pointer**
 
```c
#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;
}
```
Output:
```
/tmp/wY4tusyiIu.o
pointer2pointer 
   pointer2pointer 
   pointer2pointer 
   pointer2pointer 
   pointer2pointer

```
_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.