简体   繁体   English

如何获得有关C语言中函数调用者的信息?

[英]How can I get information about a caller of a function in C?

I have a function, void funct (struct *B) . 我有一个函数, void funct (struct *B) It will be called by some instances of struct A . struct A某些实例将调用它。 It only takes a pointer to strut B and modify that struct B instance. 它只需要指向Strut B的指针并修改该B结构实例。 If struct A has char *name . 如果struct A具有char *name My question is, can I get access to the A_instance->name ? 我的问题是,我可以访问A_instance->name吗? in the funct ? funct If yes, how? 如果是,怎么办? Thank you 谢谢

You say this is C, which leaves out member functions (C does not have member functions, classes, etc). 您说这是C,它省略了成员函数(C没有成员函数,类等)。 That means that funct is just a function (not a member of something) and that therefore, you only have the information that is passed in, and the globals. 这意味着funct只是一个函数(不是某个东西的成员),因此,您仅拥有传入的信息和全局变量。 Since neither of those things contain what you want, you can't get it. 由于这些东西都不包含您想要的东西,因此您无法获得它。

However, you also say that funct is called by 'some instances of struct A'. 但是,您也说funct由“结构A的某些实例”调用。 This doesn't make any sense, because in C structures don't have member functions, and thus can't make calls. 这没有任何意义,因为在C结构中没有成员函数,因此无法进行调用。 Either you mean that operations on some instances of struct A are calling funct (in which case, my first answer applies), or you really are working with C++, and you've got member functions in struct A. 您的意思是对struct A某些实例的操作正在调用funct(在这种情况下,我的第一个答案适用),或者您确实在使用C ++,并且在struct A中具有成员函数。

If funct is a member function of struct A, then funct has full access to all the members of the instance of A that called it, and can therefore check 'name' directly. 如果funct是struct A的成员函数,则funct具有对调用它的A实例的所有成员的完全访问权限,因此可以直接检查“名称”。 Otherwise, we're back to my first answer. 否则,我们将回到我的第一个答案。

In order to fix this, you're either going to need funct to be a member function of struct A (thereby going to C++), or you're going to need to pass the relevant information into funct. 为了解决这个问题,您要么需要funct成为struct A的成员函数(从而转到C ++),要么需要将相关信息传递给funct。

It sounds like you're trying to write a function which needs to process a name value which is present in 2 different structures: struct A and struct A . 听起来您正在尝试编写一个函数,该函数需要处理存在于2个不同结构中的name值: struct Astruct A If that's the case then why not take a char* directly in funct and have callers pass the appropriate name field? 如果是这样,那为什么不直接在函数中使用char*并让调用者传递适当的name字段呢?

funct(char * name) { 
  ..
}

funct(aInstance->name);
funct(bInstance->name);

I'm afraid C doesn't permit you access to the calling functions data. 恐怕C不允许您访问调用函数数据。 The easiest would be just to pass in a pointer to "struct A", at least then you'll always have access to A. 最简单的方法就是传递一个指向“结构A”的指针,至少这样您就始终可以访问A。

void func(struct B* b_ptr, struct A* a_ptr) 
{
    /* ... */
    /* do whatever you like with a_ptr->name */

}

Assuming you have a program like that: 假设您有一个像这样的程序:

struct B {
  int dummy;
}

struct A {
  char *name;
  struct B* b;
}

void funcA ( struct A* a ) {
   funcB ( a->b );
}

void funcB ( struct B* b ) {
   /* do something */
}

and you want to find out something about a in funcB , there is no proper way to do it. 你想了解一些关于afuncB ,有做它没有正确的方法。 What you might try to do is, by using pointer arithmetic or negative array indices, to guess the location of a on the stack, which might even work with a given compiler on a given platform. 可能想做的是通过使用指针算术或负数组索引来猜测 a在堆栈中的位置,这甚至可能与给定平台上的给定编译器一起使用。 If you make it work that way, please don't forget to post the solution to The Daily WTF . 如果您以这种方式工作,请不要忘记将解决方案发布到The Daily WTF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM