2
votes

I know that If a function has no argument & only return type (say int), then I can change my int variable by assigning the function to my variable as below,

main()
{
  int var_name;
  var_name = func();
  printf("My variable value is updated as : %d", a);
}

func()
{ return 100; }

Also I know that If I have my function's return type as void, with no arguments, then I can only print the value inside the function itself and cannot return anything in turn.

But, my doubt is, is there anything else that I can do to update my var_name by calling a function with no arguments & no return type ? ie., void func(void); by using something like pointer concepts ?? I could not able to find the exact answer for the same by my searches among so many websites.. I will be very grateful if someone can help me out finding whether I can do it by this way or not,.

Thanks,.

3
Why would you like to do that? - 5gon12eder
"by assigning the function to my variable" - nope, func() is not the function itself; it means the result of calling it. - user253751
On a side note, what decades-old tutorial are you following and why? - user253751
A local variable is private to the function. You have to use the & operator on it to make the compiler guarantee that it has an adress, otherwise it could be stored in a register or even be optimized away. - potrzebie
As of now, I am trying to understand which is the easiest way and good practice to manage memory size of my program. Hence, out of exitation, I would like to know that.. @5gon12eder - user3857354

3 Answers

1
votes

It is possible to modify a local variable in main, from a function with no arguments and no return value, if there's a global pointer to it:

#include <stdio.h>

int *p;

void func() {
    *p = 6;
}

int main() {

    int a = 5;
    p = &a;
    func();
    printf("a = %d\n", a); // prints: a = 6

    return 0;

}
0
votes

You can have global variable

int var_name;
void func();

int main()
{
   func();
   printf("%d\n",var_name);
} 

void func()
{
   var_name = 20;
}

But if your variable is local to main() then this can't be done.

There are two ways to modify the value of var_name.

  1. Make changes in the calling function and return the value.( which you have already shown)
  2. Pass the address of the var_name to the function and have pointer as arguement in the func(int *p) and modify the value inside the func()

Thats it!! No other way this can be done.

0
votes

There's no good way to do that. If you want the function to modify a local variable, you should probably change the function so it either returns a value that you can assign to the variable, or takes the variable's address as an argument.

But if you don't mind writing some ugly code, you can define a global (file-scope) pointer variable, assign the local variable's address to the global pointer, and then use that to modify the variable inside the function.

An example:

#include <stdio.h>

int *global_pointer;

void func(void) {
    *global_pointer = 42;
}

int main(void) {
    int local_variable = 0;
    global_pointer = &local_variable;
    func();
    printf("local_variable = %d\n", local_variable);
}

It's very easy to shoot yourself in the foot his way. For example, if you refer to the pointer after the calling function has terminated (and the local variable no longer exists), you'll have undefined behavior.

This technique can actually be useful if you need to make a quick temporary change in a body of code in which you can't make major interface changes. Just don't do it in code that will be maintained by anyone else -- and wash your hands afterward.