char * 指针

来源:互联网 发布:如何做好淘宝美工 编辑:程序博客网 时间:2024/05/22 00:28

经典重现:

#include <stdio.h>#include <malloc.h>#include <string.h>int main(){
   char *str = "hello,world.";   int len =  strlen(str);   char *des = (char *)malloc(len+1);   char *s = &str[len-1];   char *d = des;
  while(len--!=0)
   {        *d++ = *s--;    }   *d = 0;   printf("%s\n",des);   free(des);   return 0;}

char   *str = "hello,world."; 实为:const char *str = "hello,world." ,只能查看,不能对数组操作。char *des = (char *)malloc(len+1),在堆里分配len+1。对其操作时:要另外申请一个指针去操作。




原创粉丝点击