In my previous article, I had described the array in brief but I have received some emails from the subscriber and they want me to write an article on pointer to string array in C. If you want to read about the array, you can see my other post’s array in C/C++.
But before reading how to use the pointer to string array in C, let’s first understand the array of string (arrays of characters) and ways to create the array of string in C.
1.) String array using the 2D array:
As you know array is a collection of similar data types and all the data is stored in a contiguous memory location. So, in this case, each character will be placed at contiguous memory locations. See the below syntax,
char arr[ROW][COL]; //2d array of character
For better understanding, I have created a 2d array of characters with 5 rows and 10 columns. You can see the below image in which the address of the first element is 0 and all elements are stored in a contiguous way.
2.) 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. Let us see the syntax for the same,
char *arr[ROW]; //array of pointer to string
You can see the below image in which I have created an array of pointers to a string whose size is 5. and each pointer is pointing to the address of the first character of the 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’s see a few example codes,
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. See the below example code,
Method 1: Pointer to the 1D array:
#include<stdio.h> int main() { int row =0; //create 2d array of the characters char arr[5][10] = {"PoojaS1", "PoojaS2", "PoojaS3", "PoojaS4", "PoojaS5"}; //create pointer to the array char (*ptrArr)[10] = NULL; //initialize the pointer with array ptrArr = arr; for (row = 0; row < 5; ++row)// Loop for coloumn { printf("%s \n", ptrArr[row]); } return 0; }
Output:
Method 2: Pointer to the 2D array
#include<stdio.h> int main() { int row =0; //create 2d array of the characters char arr[5][10] = {"PoojaS1", "PoojaS2", "PoojaS3", "PoojaS4", "PoojaS5"}; //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:
You can see this article, How to access the 2d array using a pointer.
2.) Access array of string using the pointer to the array and pointer to pointer
Method 1: Pointer to the 1D array
#include<stdio.h> int main() { int row =0; //create 2d array of the characters char * arr[5] = {"PoojaS1", "PoojaS2", "PoojaS3", "PoojaS4", "PoojaS5"}; //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:
Method 2: Pointer to pointer
#include<stdio.h> int main() { int row =0; //create 2d array of the characters char * arr[5] = {"PoojaS1", "PoojaS2", "PoojaS3", "PoojaS4", "PoojaS5"}; //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:
Some Invalid Operations on an Array of Pointers to Strings:
Let’s discuss some invalid operations on an array of pointers to strings that you should avoid during the coding. Let’s consider the below-mentioned array of pointers to strings:
//array of pointers to strings char * arr[6]
When the above statement will execute, the compiler reserves 24 bytes of memory (4*6) to store 6 pointers of type char (By assuming the size of the pointer is 4 bytes), but it doesn’t allocate any memory for a string literal.
Because we have not initialized the array with valid string literals and have not allocated a valid memory to store the string. So each index of the array contains a garbage value and may be pointing to anywhere in the memory.
So for the mentioned array below operation will be invalid, if we perform the below-mentioned operation on the array we got an undefined result.
Operation 1: strcpy(arr[0], "Aticleworld"); // invalid Operation 2: strcat(arr[0], "Aticleworld"); // invalid Operation 3: gets(arr[0]); // invalid Operation 4: fgets(arr[0],10,stdin) //invalid Operation 5: scanf("%s", arr[0]); // invalid
Recommended Articles for you:
- How to create a dynamic array in C?
- How to pass an array as a parameter in C?
- A brief description of the pointer in C.
- Introduction of Array in C.
- Dangling, Void, Null, and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- How to access 2d array in C?
- void pointer in C.
- 10 questions about dynamic memory allocation.
- How to access a two-dimensional array using pointers in C?
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.
- C format specifiers.
- C operators you should know.