How to change value of arguments in the function

后端 未结 4 527
南旧
南旧 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:48

    You are changing the pointer value in foo by the malloc call - just do this:

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

    In your code - this function:

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

    Works as follows:

    +----+
    | a  |
    +----+
    /
    x points here
    

    Then you make x point somewhere else:

    +----+                            +----+
    | 5  | /* created by malloc*/     | a  |
    +----+                            +----+
    / On Heap                         / On Stack
    x now points here                a is now in memory somewhere not getting modified.
    

    And you make it 5. Also note - x is a local copy of the &a address of a - in C there is no pass by reference but only pass by value - since the variables value is copied into the function.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-17 07:01
    • foo(&a); Passes the address of a variable to a function.

    • void foo(int* x) That address passed is now copied and stored in x. x is a pointer variable local to the function.

    • x = (int *)malloc(sizeof(int));. Here you overwrite the contents of x with a completely different address, to dynamically allocated memory.

    • *x = 5; Here you assign the value 5 to that memory, which resides on the heap.

    • } Here you forgot about everything you did in the function. You create a memory leak since you didn't free() the memory. Apart from that, the function has done nothing that affects the rest of the program. It only fiddled around with the local variable x.

    • printf("%d\n",a); Here you print the contents of the uninitialized variable a, which has not been altered by the function.

    In addition, it never makes sense to cast the result of malloc in C. Also, malloc is found in stdlib.h. Don't use malloc.h, that's a superfluous, non-standard header.

    0 讨论(0)
  • 2021-01-17 07:10

    In foo, variable x is a local variable, a pointer to an int - it is a variable containing a value which is the address of an int in memory.

    You are calling the function with the address of main's a variable. But then you are changing x

    x = (int *)malloc(sizeof(int));
    

    This changes x to be a different value, the address of an area of memory now reserved for our use. HOWEVER, x is a variable local to foo. When foo ends, x goes away, and we return to main. Two things have happened:

    1. We have allocated some memory,
    2. We have assigned a value to it.

    You then print out the contents of mains a. a is still at the same location it was before we called foo.

    Perhaps this previous answer to a related question will help you: Pointer errors in the method of transmission(c++)

    0 讨论(0)
提交回复
热议问题