#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { charPointerToCharArray(); return 0; } void charPointerToCharArray() { char *str="Reflection,meditation,practice!"; int size=strlen(str); printf("str=%s,size=%u\n",str,size); char chArr[size]; for(int i=0;i<size;i++) { chArr[i]=str[i]; } for(int i=0;i<size;i++) { printf("%c\t",chArr[i]); } printf("\nchArr=%s\n",chArr); } void charPointer1() { char *msg="Hello C!"; int size=sizeof(msg); for(int i=0;i<size;i++) { printf("%c\n",msg[i]); } char chArr[size]; for(int i=0;i<size;i++) { chArr[i]=msg[i]; } printf("Arr=%s\n",chArr); }
Compile:
gcc -g h1.c -o h1
Run via execute ./h1,and the result as below.