Assign string value to char pointer and char array - C String

C examples for String:char array

Description

Assign string value to char pointer and char array

Demo Code

#include <stdio.h>
#include <string.h>

int main()//from w ww. j  a va  2 s . c om
{
    char Kid1[12];
    char Kid2[] = "kid name 1";
    char Kid3[7] = "Andrew";
    char Hero1[] = "Batman";
    char Hero2[34] = "Spiderman";
    char Hero3[25];

    Kid1[0] = 'K'; 
    Kid1[1] = 'a'; 
    Kid1[2] = 't';
    Kid1[3] = 'i';
    Kid1[4] = 'e';
    Kid1[5] = '\0'; 
    strcpy(Hero3, "The Incredible Hulk");

    printf("%s\'s favorite hero is %s.\n", Kid1, Hero1);
    printf("%s\'s favorite hero is %s.\n", Kid2, Hero2);
    printf("%s\'s favorite hero is %s.\n", Kid3, Hero3);

    return 0;
}

Result


Related Tutorials