Novice C language learning journey(10)

in hive-169321 •  5 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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Good post

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