how to access local and global vars in a function 
Author Message
 how to access local and global vars in a function

Please help me to find the solution to this problem in C

int i=10;  /* declares i as a global variable */
main()
{
 int i=5;
  printf("%d",i);

Quote:
}

this will print 5 as local variable gets preference over global i=10.
Please suggest a way to access global variable in current situation
Thanks

--
Posted via CNET Help.com
http://www.*-*-*.com/



Tue, 03 Dec 2002 03:00:00 GMT  
 how to access local and global vars in a function

Quote:

>Please help me to find the solution to this problem in C

>int i=10;  /* declares i as a global variable */
>main()
>{
> int i=5;
>  printf("%d",i);
>}

>this will print 5 as local variable gets preference over global i=10.
>Please suggest a way to access global variable in current situation

You're kidding, right? Name the local variable something else! And
avoid single letter vars. when possible, especially if they have more
than function scope.

C will always use the most local scope of the variable name, in this
case the local automatic variable 'i = 5' will be used. It will always
mask the globals variable 'i = 10'. This is extremely bad practice
to do.



Tue, 03 Dec 2002 03:00:00 GMT  
 how to access local and global vars in a function

Quote:

> Please help me to find the solution to this problem in C

> int i=10;  /* declares i as a global variable */

                                ^^^^^^^^^^^^^^^
  ITYN 'variable with file scope and external linkage'

Quote:
> main()

  In the current C standard, main must be declared to return an int.
  Even before the current standard, implicit int was evil.

Quote:
> {
>  int i=5;
>   printf("%d",i);

  printf is variadic, yet you have not #included <stdio.h> or provided
  a prototype.

  Under the current standard, you can get away with not returning an
  int from main().  This was not true in the previous standard and is
  in any case a poor practice not to provide a return value from a
  function that promises one.

Quote:
> }

> this will print 5 as local variable gets preference over global i=10.
> Please suggest a way to access global variable in current situation

Don't shadow variables defined in an enclosing scope.
There is a language designed for such sloppy coding.  It is called C++.

--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer



Tue, 03 Dec 2002 03:00:00 GMT  
 how to access local and global vars in a function

int * give_me_a_pointer_to_global_i(void)
{
   return &i;

Quote:
}
> int i=10;  /* declares i as a global variable */
int
> main(
void
> )
> {
>  int i=5;
>   printf("%d\n",i);

    printf("%d\n",*give_me_a_pointer_to_global_i());

Quote:
> }

Or just give one of the "i" variables a different name.

Bill, a thing-in-itself.



Tue, 03 Dec 2002 03:00:00 GMT  
 how to access local and global vars in a function

Quote:

>Please help me to find the solution to this problem in C

>int i=10;  /* declares i as a global variable */
>main()
>{
> int i=5;
>  printf("%d",i);
>}

>this will print 5 as local variable gets preference over global i=10.
>Please suggest a way to access global variable in current situation

You can't. The local definition masks the global one. The only way
around this is to give them different names.

--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Thu, 05 Dec 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. global vars vs: local vars

2. global vs local vars

3. Global vs. Local vars and .MAP

4. Accessing vars in global and module scope in ANSI C

5. Accessing vars in global and module scope in ANSI C

6. Accessing vars in global

7. static global vars and functions

8. Using vars to access structures/functions

9. basic question about pointers and local vars

10. VC6 SP3 debugger, local vars not recognized

11. global/static vars inside loops

12. How can I access local variable from a different function

 

 
Powered by phpBB® Forum Software