Novice C language learning journey(10)

in Programming & Dev5 years ago

梦幻光晕蓝色背景矢量图.png
Take a sting constant "efgh" declare as character pointer type.take the value of "efgh" pass to anther "a" string constant.The "a" string constant is declared as character pointer type too.
But it did not shows the output of "efgh" on console.Because the "a" pointer has not point to the "c" array first address after executed the "for" statement.
The code as follows"

   #include"stdio.h"
   #include"stdlib.h"
   main(){
   char *a="abcd";
   char *b="efgh";
   char c[10];
   int i=0;
   for(;*b!='\0';b++,a++,i++){
          c[i]=*b;
          a=&c[i];  
   }
   printf("%s\n",a);
   system("pause");
   }

1.PNG

So how to modify?
The modified as follows:

  #include"stdio.h"
  #include"stdlib.h"
  main(){
  char *a="abcd";
  char *b="efgh";
  char c[10];
  int i=0;
  for(;*b!='\0';b++,a++,i++){
         c[i]=*b;
  }
  c[i]='\0';
  a=c;
  printf("%s\n",a);
  system("pause");
  }

2.PNG

Sort:  

Thank you for continuing to share your journey on the Programming Community. Upvoted!

 5 years ago  Reveal Comment