Pointer to string array in C

pointer to string array in c

In my previous article, I had described the array in brief but I have received some email from the subscriber and they want me to write an article on string array in C. If you want to read the array, you can see my article brief introduction of the array.

We can create the string array in C  (arrays of characters) using the 2d array of characters or array of pointer to string.

  • char arr[ROW][COL]; //2d array of character
  • char *arr[ROW]; //array of pointer to string

Before accessing the string array with pointer let see the representation of the string array using the above two described ways.

String array using the 2D array:

As we know the array is a collection of the similar data type and all the data stored in the contiguous memory location. So, in this case, each character will be placed at contiguous memory locations. See the below example,

you can see the below image in which address of the first element is 0 and all elements are stored in a contiguous way.

Pointer to string array in C

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 first character of the string.

string array in C

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

 

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 few example code,

Access 2d array of characters using the pointer to the array

To access the array of string 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. See the below example code,

Method 1: Pointer to the 1D array

 

pointer to string in C

 

Method 2: Pointer to the 2D array

 

pointer to string in C

 

You can see this article, How to access the 2d array using a pointer.

Access array of string using the pointer to the array and pointer  to pointer

Method 1: Pointer to the 1D array

 

 

Method 2: Pointer to pointer

 

 

Leave a Reply