zoukankan      html  css  js  c++  java
  • 形参传递关键点

    ----

     不能传递, str还是0;

    str值为1;

    如下例子:

      #include <stdio.h>
      #i nclude <stdlib.h>
      void getmemory(char *p)
      {
        p=(char *) malloc(100);
        strcpy(p,"hello world");
      }
      int main( )
      {
        char *str=NULL;
        getmemory(str);
        printf("%s/n",str);
        free(str);
        return 0;
       }
    程序崩溃,getmemory中的malloc 不能返回动态内存, free()对str操作很危险

    -形参 同级别,不能传递;

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
     
    void GetMemory1(char **p)
    {
          *p = (char *)malloc(100);
    }
     
    void main()
    {
          char *str = NULL;
          //GetMemory1(str);
        GetMemory1(&str);
          strcpy(str, "hello world");
          printf(str);
    }

    这样可以;

    这其实是一个参数传递的问题.
    修改变量值需要传递该变量类型的一级指针;
    修改一级指针指需要传递对应类型的二级指针.

    ----

  • 相关阅读:
    渐变
    阴影
    html+css
    background用法
    语言特点
    h5c3介绍
    js的组成
    第九章 查找文件或者命令
    第八章 查看文件内容命令
    第七章 文件管理之基础命令
  • 原文地址:https://www.cnblogs.com/Ph-one/p/8510772.html
Copyright © 2011-2022 走看看