Copy the array of char pointers to a buffer/char array

We learnt that we can easily print the array of char pointers discussed here

Now, I wanted to print all of the array values in a single buffer. Let's start.



#include<stdio.h>
#define MAX_INDEX 3 
#define BUF_LEN 100

void main() 
{    
    int i = 0;
    char arr1[10] = "Shirley";
    char arr2[10] = "Sivy";
    char arr3[10] = "Sweety";
    char *arr_ptr[MAX_INDEX] = {NULL}; 
    char buf[BUF_LEN] = {'\0'};
    int len = 0;

    printf("arr1 %s, arr2 %s arr3 %s\n", arr1, arr2, arr3);

    arr_ptr[0] = arr1;
    arr_ptr[1] = arr2;
    arr_ptr[2] = arr3;

    for(i = 0; i < MAX_INDEX; i++) {
        snprintf(buf+len, sizeof(buf) - len, "%s ", arr_ptr[i]);
        len = strlen(buf);
        printf("arr_ptr[%d] %s Buffer %s\n", i, arr_ptr[i], buf);
    }
    return;
}


1
2
3
4
arr1 Shirley, arr2 Sivy arr3 Sweety
arr_ptr[0] Shirley Buffer Shirley 
arr_ptr[1] Sivy Buffer Shirley Sivy 
arr_ptr[2] Sweety Buffer Shirley Sivy Sweety 

Can we store integers in char array ?? 

Of course. Yes. 

Ensure it is assigned with single quotes. 
  
char stairs[size] = '\0'; // Variable Declaration with Initialisation

stairs[start] = '1';   // Store an integer value in the char array





Comments