I need to access the value of variable a which is defined in main function without passing it as argument.
main()
{
int a=10;
func();
printf("%d\n",a);
}
void func(){
//i need access of variable a here.
}
How can i do this?
I need to access the value of variable a which is defined in main function without passing it as argument.
main()
{
int a=10;
func();
printf("%d\n",a);
}
void func(){
//i need access of variable a here.
}
How can i do this?
You can pass pointer to a
to your function. Pointers to local variables are valid as long as corresponding local variable exists.
So
#include <stdio.h>
void func(int *ptr);
main()
{
int a = 10;
// Pass pointer to a
func(&a);
printf("%d\n", a); // Prints 12
}
// function accepts pointer to variable of type int
void func(int *ptr)
{
// update value behind the pointer
*ptr = 12;
}
You cannot do that since that variable might not even exist then func()
is called. In your example the compiler will most probably optimize it out and effectively create this code:
main()
{
func();
printf("%d\n",10);
}
If you were sure the variable was not optimized out, it might very well be stored in some register. That register, if you knew which one it was, could perhaps be accessible from func()
or it might be stored on the stack. But if you were sure that a
was actually allocated in the stackframe of main()
you could dig down in the stack searching for a pointer to main()
and access the stackframe of main()
. But you still wouldn't know the position of a
in the stackframe.
If this is a purely theoretical question (as suggested in the comment) an answer could be to store a pointer to a
in a global variable. (Although I'm unable to come up with any kind of real world scenario where this solution has any practical benefit).
static int *ptr;
main()
{
int a=10;
ptr=&a;
func();
printf("%d\n",a);
}
void func(){
// update value behind the pointer
*ptr=12;
}
Local variables are stored in stack and that will have an address in stack. What we can do is start checking for stack pointer address by varying it until you get the same address as of local variable and then you can do the required operation on it. Of course this is not the correct way of c programming but this is the only method to do it. Below is the code.
Note: The value TriedAndTested is taken as 13 because i got the address after incrementing the stack pointer for 13 times. It might vary in other compilers. Instead one can compare the address and then evaluate it.
#include<stdio.h>
#define TriedAndTested 13 //keep incrementing until you get the value of a
void func(){
void *p=NULL;
int *st;
int i=0;
st=(void *)&p;
printf("p is %ld\n",st);
for(i=0;i<TriedAndTested;i++){
st++;
printf("p is %ld\n",*st);}
printf("p is %ld\n",++(*st)); //here i am incrementing the value of a
}
int main(){
int a = 89; //trying to change the value of a in func()
func();
printf("%ld -> %d\n",&a,a);
return 0;
}
No, because each local variable is pushed into the stack frame of the respective function and each function can only access its own stack frame with the current frame pointer.
Note: This is valid with ARM GCC 9.2.1