Please consider the below code.
#include<stdio.h>
#include<string.h>
void main()
{
char a[6], b[6];
strcpy(a,"rajeev");
printf("print A:");
for(int i=0;i<strlen(a);i++)
{
printf("%c",a[i]);
}
strcpy(b,a);
printf("print B:");
for(int i=0;i<strlen(b);i++)
{
printf("%c",b[i]);
}
printf("trying to print A again");
for(int i=0;i<strlen(a);i++)
{
printf("%c",a[i]);
}
While running this program,in the "trying to print A again" section prints nothing, and the strlen(a)
will be 0. That means the source array will be empty.
Can you please help me to understand the phenomena behind this?
But, change the declaration of a[6]
to char* a=malloc(6)
works properly.