This post is such an interesting day-to-day coding for all us. Click here for Array of Integer Pointers.
When you are asked to assign values to array of char pointers and printing them in an interview panel, you will go crazy. Chill!! Read this post before an interview ;-)
This post covered the below Coding :
- Assigning values to array of char pointers
- Copying array of char pointers to another array
- Array of pointers declaration
- Print the array of char pointers
- Printing buffered lines to console before collecting coredump
Assigning value to array of char pointers
#include<stdio.h> void main() { char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[4] = {NULL}; // or can be char *arr_ptr[] = {NULL, NULL, NULL, NULL} printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); return; }
Output
arr1 Shirley, arr2 Vicky arr3 Sivy arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] Sivy
How to copy an array of pointers to another array
#include<stdio.h> void main() { char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[4] = {NULL}; char buf[10] = {0}; printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); strncpy(buf, arr_ptr[2], 10); printf(" Buffer %s\n", buf); return; }
Output :
arr1 Shirley, arr2 Vicky arr3 Sivy arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] Sivy Buffer Sivy
Array of Pointer Declaration
It is likely to iterate over the array quite easily without knowing exactly how many elements there are in the array. This happens when an array either doesn't hold the size or not initialized in declaration like below.
char *disney[] = {NULL};
#include<stdio.h> void main() { char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[] = {NULL}; printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); return; }
Output :
An array with proper initialization or with proper size, will not yield out-of-bound error.arr1 Shirley, arr2 Vicky arr3 Sivy arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] ���_��
char *disney[] = { "Tangled", "Upsidedown", "Despicable Me", "Cinderalla Story", "Rio", "Brave", "Tin-Tin", NULL };
Print the array of char pointers
- In any array of pointers, the last array should be NULL if you copied the array of pointers to other double pointers.
- Iterating the double-pointer without checking the array size, might produce segmentation fault (eg: below program), when you access invalid memory.
- It is not necessary to initialize the array of pointer more than array size always.
#include<stdio.h> void main() { int i = 0; char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[3] = {NULL}; char **buf = arr_ptr; printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; /* Here, Only indices 0, 1 & 2 are accessed, so no segmentation fault. */ printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); for(i=0; *buf; i++, buf++) { /* After crossing index 2, buf might contain invalid memory * as index 3 wasn't initialized to NULL. Only indices 0, * 1, and 2 are initialized. */ printf("arr_ptr[%d] %s\n", i, *buf); fflush(stdout); /* If not added, valid memory values will not be displayed */ } return; }
Output:
arr1 Shirley, arr2 Vicky arr3 Sivy
arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] Sivy
arr_ptr[0] Shirley
arr_ptr[1] Vicky
arr_ptr[2] Sivy
Segmentation fault |
How to print the buffered lines to console before collecting coredump
Before printing the first 2 printf's, it will start collecting the core dump. Therefore, the first 2 printf's will not be displayed in console. But do you want to know which line causes the segmentation fault ? Use fflush().
#include<stdio.h> void main() { int i = 0; char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[3] = {NULL}; char **buf = arr_ptr; printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); fflush(stdout); for(i=0; *buf; i++, buf++) { printf("arr_ptr[%d] %s\n", i, *buf); } return; }
Output
arr1 Shirley, arr2 Vicky arr3 Sivy arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] Sivy Segmentation fault
Print the array of char pointers
#include<stdio.h> void main() { int i = 0; char arr1[10] = "Shirley"; char arr2[10] = "Vicky"; char arr3[10] = "Sivy"; char *arr_ptr[4] = {NULL}; char **buf = arr_ptr; printf("arr1 %s, arr2 %s arr3 %s\n",arr1, arr2, arr3); arr_ptr[0] = arr1; arr_ptr[1] = arr2; arr_ptr[2] = arr3; printf("arr_ptr[0] %s, arr_ptr[1] %s, arr_ptr[2] %s\n",arr_ptr[0],arr_ptr[1],arr_ptr[2]); for(i=0; *buf; i++, buf++) { printf("arr_ptr[%d] %s\n", i, *buf); } return; }
Output
arr1 Shirley, arr2 Vicky arr3 Sivy arr_ptr[0] Shirley, arr_ptr[1] Vicky, arr_ptr[2] Sivy arr_ptr[0] Shirley arr_ptr[1] Vicky arr_ptr[2] Sivy
Click here to display all the values of the array in a single buffer. (string concatenation)
Comments
Post a Comment