Store Strings Without Initialization using a pointer to char and a char array - C String

C examples for String:char array

Description

Store Strings Without Initialization using a pointer to char and a char array

Demo Code

#include <stdio.h>
#include <string.h>
int main()/*from   www . j a  v  a 2 s  .  c o m*/
{
     char name[8] ;
     char *pname ;
    
     strcpy(name, "book2s.com");
     pname = "book2s.com";
    
     printf("\nname: %s\n", name);
     printf("pname: %s\n", pname);
    
     strcpy(name, "book2s.c o m");
     pname = "book2s.c o m";
    
     printf("name: %s\n", name);
     printf("pname: %s\n", pname);
    
     printf("name (all eight bytes):  ");

     for(int i = 0; i < 8; i++) {
         printf("%c ", name[i]);
     }
    
     printf("\npname (all eight bytes):  ");
     for(int i = 0; i < 8; i++) {
         printf("%c ", *(pname + i));
     }
    
     return(0);
}

Result


Related Tutorials