美文网首页
9-malloc练习-大小端

9-malloc练习-大小端

作者: ibo | 来源:发表于2017-02-03 15:43 被阅读0次
    malloc 练习 :
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void getmemory(char *p)
    {
        p = (char *)malloc(100);
    }
    int main(int argc, const char *argv[])
    {
        char *str = NULL;
        getmemory(str);  //  注意 : 此时函数传参 的为 变量str的值  .
        strcpy(str,"hello world");
        printf("%s\n",str);
        free(str);
        return 0;
    }
    

    大小端判断 :
    #include <stdio.h>
    int sys_check(){
      int num = 1;//定义一个变量值为1
      char *p = (char *)&num//将地址值强制转化为char *类型 并赋给值首地址赋值给指针p
       if(*p == 1)//看是不是为1
        return 1; //小端存储
      else
        return 0; //大端存储
    }
    
    int main(){
      int ret = sys_check();
      if(ret == 1){
        printf("little\n");
      }
      else if(ret == 0){
        printf("big\n");
      }
      system("pause");
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:9-malloc练习-大小端

          本文链接:https://www.haomeiwen.com/subject/kmydittx.html