Code :
int a = 33;
int main()
{
int a = 40; // local variables always win when there is a conflict between local and global.
// Here how can i access global variable 'a' having value '33'.
}
If you ask : Why would someone want to do above thing? Why [a-zA-Z]* ?
My answer would be : Just to know that 'it is possible to do so'.
Thanks.
How about this old trick:
int main()
{
int a = 40; // local variables always win when there is a conflict between local and global.
{
extern int a;
printf("%d\n", a);
}
}
int a = 33;
int main()
{
int a = 40;
int b;
{
extern int a;
b = a;
}
/* now b contains the value of the global a */
}
A harder problem is getting a
if it's static
with file scope, but that's also solvable:
static int a = 33;
static int *get_a() { return &a; }
int main()
{
int a = 40;
int b = *get_a();
/* now b contains the value of the global a */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With