Access restriction for external variable..

This is a discussion on Access restriction for external variable.. within the C Programming forums, part of the General Programming Boards category; Is there any way to restrict access to external variable with in a function? here is a code like: how ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Thumbs down Access restriction for external variable..

    Is there any way to restrict access to external variable with in a function?

    here is a code like:
    how we can restrict integer i accesibility within the func1?


    extern int i;
    func1()
    (

    }


    thanks in advance
    Bhupesh....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Declare i to be a local variable in func1() instead of a global variable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Post

    Hi Laserlight ...

    thanks for reply....but still i dint get that...can u plz pass some more light on this...

    Thanks
    Bhupesh

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The point is that an external variable IS external, so it is available everywhere.

    You can put the extern declaration INSIDE a function, e.g.
    Code:
    func1()
    (
      extern int i;
      ...
    }
    This of course isn't going to prevent some other function from doing exactly the same, so it's only going to make sure that you KNOW which functions have direct access to the variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry for the double post. On top of what I wrote above, there could be any number of OTHER solutions that you could use to restrict the access so that you can only get hold of some "global" data in the right way. You could for example have a local static variable that you return the address of in a particular function - of course, there's still nothing preventing this function from being called from multiple places.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21