Ivan Gotovchits <iv*************@auriga.ruwrites:
anto frank wrote:
> is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?
void main()
{
int a=10;
fun();
}
fun( )
{
printf("%d", a);
}
condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments
I think that the idea is to access it through stack. Variable a lives in
stack in a range of 4 bytes, because there is a Return Address. (It may be
wrong, I'm not sure).
So you can try to address it in this way:
int
main(void)
{
int a = 10;
func();
}
int
func(void)
{
int *p;
p -= 2; /* a return address and p itself (8 bytes total) */
printf("%d\n", *p);
}
I am not sure that it will work, but you can try it. And if it fails, then
google for `buffer overflow'.
I'm afraid your proposed solution is nonsense. The variable 'p' is
uninitialized. Its initial value is garbage; even examining it
invokes undefined behavior. You assume that ints and pointers are the
same size, something that is not guaranteed by the language (and it's
untrue on several systems I use every day). Even if that were valid,
you make several unsupportable assumptions about stack layout (there
might not even be a stack in the usual sense).
Did you try it yourself? Why not?
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"