C-Change the value using a function without the parameter pass-in.
in Solutions with 0 comment

C-Change the value using a function without the parameter pass-in.

in Solutions with 0 comment

C-Change the value using a function without the parameter pass-in.

Description

#include "stdio.h"
void pass(){}

int main()
{
    int x = 123;
    pass();
    printf("%d\n",x);
    getchar();
}

Give the implemention of pass() to change the value of x to 456 and print it.

Solution

When the process running. The variable x keep int the stack part of the memory. So we could make a new varible (such like int temp) in the lower address of the x and use a pointer to get its location. Then we just find the higher address until we find the x and then change the value of the x.

void pass()
{
    int tmp=0;
    int* p=&tmp;
    while(*p!=123)++p;
    *p=456;
}

Solved.

Responses