How to change value of arguments in the function

后端 未结 4 529
南旧
南旧 2021-01-17 06:17

I try it :

#include 
#include 

void foo(int* x)
{
   x = (int *)malloc(sizeof(int));
   *x = 5;
}

int main()
{

   int a;
           


        
4条回答
  •  逝去的感伤
    2021-01-17 06:52

    Pointer x is already initialized in formal arguments, malloc will create x pointing to another memory location.

    void foo(int* x)
    {
     /* x = (int *)malloc(sizeof(int)); */
      *x = 5;
    }
    

    Please look at Acme's answer for very good diagrammatic representation.

提交回复
热议问题