Possible Duplicate:
Is it possible to find out the variable name, the pointer pointing to?
Is it Possible to get the name of array the pointer pointing too?
example:
char name[20];
char *p=name
int door_no;
int *q= & door_no
In the above example we are giving the base address of the array with the array name and pointer q pointing to door_no but what if, I have to know the name of the variable the array pointing too? what is t开发者_开发问答he variable name pointer q is pointing too ? Is it possible I made tries and I came to a conclusion no its not possible but still Iam trying to get the solution. and what you think guys ? Is there any way to make it possible?
Short answer: No.
Long Answer:
In C variable names do not exist after compilation (everything is converted to either memory locations or register locations).
The compiler may potentially assign multiple variables to the same memory/register location if their lifespans of the two objects do not overlap. Thus the concept of variable names at runtime has no meaning (in the context of C)
No, it's not possible. Your pointer holds an address to a space in memory. The variable name is a name you have defined to mean that space in memory. So, you have somthing like this:
name -> mem space
q ------^
There is nothing linking q to name, and they don't go backwards. Both of them will get you to memory space, but you can't map back to them. Also, the program does not know that these happen to map to the same thing! The only reason you know is because you set it up to do so.
It is possible, but only within the scope of the original object.
You gave these examples (I've modified the formatting slightly):
char name[20];
char *p = name;
int door_no;
int *q = &door_no;
You can do comparisons equivalent to your assignments, thus:
if (p == name)
printf("p == name\n");
if (q == &door_no)
printf("q == &door_no\n");
This started as a comment, but then I realized it probably works better as an actual answer.
The short answer to the question as stated is, as others have already pointed out, no, you cannot map from a memory location to a variable name corresponding to that location.
Think about what would happen if it actually was possible to do something like what you want. Here is a thought experiment for you. I'm assuming C, but it should apply equally well in any language that supports pointers to arbitrary memory locations (including memory locations occupied by other variables).
Suppose that you start out with two pointers to an identical memory location:
char p[] = DUMMY;
char *q = &p;
Now, if you were to somehow de-dereference &q
, you'd get p
. Okay, that kinda-sorta works, in the theoretical sense. Now, add this:
char *r = &q;
Now, you have double indirection. If you try to figure out what names point to the same memory location as the name p
, what is the result? If I recall the pointer syntax correct, you get *q
and *(*r)
. How is the compiler going to tell you that, particularly at runtime?
Suppose that p[]
is sufficiently large. For an arbitrary number n
, replace the *q
assignment by:
char *q = &p + n;
What is now the result of similarly de-dereferencing q
? What would be the result in case q
now points outside the bounds of p[]
? (Okay, that latter could conceivably be just q
, but then what's the point?)
Or, a practice which was quite common before the wide advent of GUIs. Try this (never mind the actual address).
short *videomemory = 0xB8000;
/* and then, at some later point... */
videomemory += 4;
Then, try to find the address corresponding to the value of videomemory
(0xB8004, given the above). What is the result? In principle, the "array" videomemory[]
extends indefinitely, even wrapping around (let's ignore memory protection for the moment), and thus anything will map into the memory "occupied" by videomemory[]
, only given a large enough array index.
It is not possible to get the name of the variables p
or q
point to if you compile and execute the program traditionally, because one of the things the compiler does is forget the name of the variables, keeping only addresses.
Depending on what you are trying to do, you may execute the program in a non-traditional execution environment where the names are preserved. For instance,
~ $ cat t.c main(){ char name[20]; char *p=name; int door_no; int *q= & door_no; } ~ $ frama-c -val t.c [kernel] preprocessing with "gcc -C -E -I. t.c" ... [value] ====== VALUES COMPUTED ====== [value] Values for function main: p ∈ {{ &name ;}} q ∈ {{ &door_no ;}} __retres ∈ {0; }
精彩评论