Skip to content
Home » How to use char * datatype in C

How to use char * datatype in C

Learn about C Program to use char * datatype in C in the below code example.
Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to use char * datatype in C:

char * datatype is defined to be array of pointers for this ,It should be defined using malloc for dynamically allocating memory else it will return (null) value.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i;
    char *a[10];//declaring array of pointers
    for(i=0;i<3;i++)
    {
        a[i] = (char*) malloc( 10* sizeof(char));//dynamically allocate memory
        printf("Enter details of %d:",i+1);
        scanf("%s",a[i]);
    }
    for(i=0;i<3;i++)
    {
        printf("%s\t",a[i]);
    }
}

Enter details of 1:Welcome
Enter details of 2:to
Enter details of 3:SimilarGeeks
Welcome to SimilarGeeks

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.