![](/img/trans.png)
[英]how to pass a pointer to struct to another function in C, in calling function if I have access to only pointer variable
[英]In C language, can I access local variable of main function in another function through stack pointer?
我需要访问在 main 函数中定义的变量 a 的值,而不将其作为参数传递。
main()
{
int a=10;
func();
printf("%d\n",a);
}
void func(){
//i need access of variable a here.
}
我该怎么做?
您可以将指向a
指针传递给您的函数。 只要相应的局部变量存在,指向局部变量的指针就是有效的。 所以
#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;
}
您不能这样做,因为该变量甚至可能不存在,然后func()
被调用。 在您的示例中,编译器很可能会优化它并有效地创建此代码:
main()
{
func();
printf("%d\n",10);
}
如果您确定变量没有被优化掉,它很可能被存储在某个寄存器中。 那个寄存器,如果你知道它是哪个,也许可以从func()
访问,或者它可以存储在堆栈中。 但如果你是确保a
实际上是在通过StackFrame分配main()
你可以在堆栈中向下搜索挖掘指针main()
和访问的StackFrame main()
但是您仍然不知道a
在堆栈帧中的位置。
如果这是一个纯粹的理论问题(如评论所说)的答案可能是一个存储指向a
全局变量。 (尽管我无法想出该解决方案具有任何实际好处的任何现实世界场景)。
static int *ptr;
main()
{
int a=10;
ptr=&a;
func();
printf("%d\n",a);
}
void func(){
// update value behind the pointer
*ptr=12;
}
局部变量存储在堆栈中,并且在堆栈中有一个地址。 我们可以做的是通过改变堆栈指针地址开始检查堆栈指针地址,直到获得与局部变量相同的地址,然后您可以对其进行所需的操作。 当然,这不是 c 编程的正确方法,但这是唯一的方法。 下面是代码。
注意:TriedAndTested 的值取为 13,因为我在将堆栈指针递增 13 次后得到了地址。 它可能在其他编译器中有所不同。 相反,可以比较地址,然后对其进行评估。
#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;
}
不,因为每个局部变量都被推入相应函数的堆栈帧中,并且每个函数只能使用当前帧指针访问自己的堆栈帧。
注意:这对 ARM GCC 9.2.1 有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.