scope resolution operator???????

Discussion in 'C Programming' started by sushant, Jan 7, 2005.

  1. sushant

    sushant Guest

    hi ,

    can we use scope resolution operator :):) in C???

    sushant
     
    sushant, Jan 7, 2005
    #1
    1. Advertising

  2. sushant wrote:
    >
    > hi ,
    >
    > can we use scope resolution operator :):) in C???


    No, that is a c++ specific kludge.

    Erik

    --
    +-----------------------------------------------------------+
    Erik de Castro Lopo (Yes it's valid)
    +-----------------------------------------------------------+
    "To me C++ seems to be a language that has sacrificed orthogonality
    and elegance for random expediency." -- Meilir Page-Jones
     
    Erik de Castro Lopo, Jan 7, 2005
    #2
    1. Advertising

  3. sushant wrote:
    > can we use scope resolution operator :):) in C???


    The only reason to do so would be to access some member of a namespace
    or class - neither of which C has. What application did you have in mind?
    --
    Derrick Coetzee
     
    Derrick Coetzee, Jan 7, 2005
    #3
  4. (sushant) writes:
    > can we use scope resolution operator :):) in C???


    No.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 7, 2005
    #4
  5. sushant

    infobahn Guest

    sushant wrote:
    >
    > hi ,
    >
    > can we use scope resolution operator :):) in C???


    No, C doesn't have that operator.
     
    infobahn, Jan 7, 2005
    #5
  6. sushant

    Guest

    i want to access the value of global variable inside the main function
    and inside the main func the var with the same name is available..for
    eg.

    int x=20;

    main()
    {
    int x=10;
    printf("%d",x);

    }
    i want the o/p to be 20.. how can v achieve that?
    Derrick Coetzee wrote:
    > sushant wrote:
    > > can we use scope resolution operator :):) in C???

    >
    > The only reason to do so would be to access some member of a

    namespace
    > or class - neither of which C has. What application did you have in

    mind?
    > --
    > Derrick Coetzee
     
    , Jan 7, 2005
    #6
  7. Derrick Coetzee wrote:
    >> can we use scope resolution operator :):) in C???

    >
    > The only reason to do so would be to access some member of a namespace
    > or class - neither of which C has.


    It can also be used to access a hidden global name from a local scope.
    Theoretically, for this very purpose '::' could be useful in C as well.

    I'm not saying though that this is enough to justify the need for '::'
    operator in C. It is not.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jan 7, 2005
    #7
  8. sushant

    Ben Pfaff Guest

    writes:

    > i want to access the value of global variable inside the main function
    > and inside the main func the var with the same name is available..for
    > eg.
    >
    > int x=20;
    >
    > main()
    > {
    > int x=10;
    > printf("%d",x);
    >
    > }


    main()
    {
    int x = 10;
    {
    extern int x;
    printf ("%d", x);
    }
    ...
    }
    --
    "The expression isn't unclear *at all* and only an expert could actually
    have doubts about it"
    --Dan Pop
     
    Ben Pfaff, Jan 7, 2005
    #8
  9. sushant

    Taran Guest

    wrote:
    > i want to access the value of global variable inside the main

    function
    > and inside the main func the var with the same name is available..for
    > eg.
    >
    > int x=20;
    >
    > main()
    > {
    > int x=10;
    > printf("%d",x);
    >
    > }
    > i want the o/p to be 20.. how can v achieve that?

    NO.

    Whenever there's a name clash between a local and global variable the
    local variable name and value is recognized.
    AFAIK there's isn't any way to access global variable with name clashes
    with a local vairable. I knew this so I haven't ever used same name in
    local scope, my company's coding guidelines suggest we don't do that ;)
    , so I never tried accessing the global variable, so I'm not aware even
    it exists.

    HTH.
    Regards,
    Taran
     
    Taran, Jan 7, 2005
    #9
  10. wrote:
    > i want to access the value of global variable inside the main function
    > and inside the main func the var with the same name is available..


    Ah, I didn't think of this. Another reply indicated one way of doing
    this. However, this is purely academic, since if this situation ever
    arose in practice you should rename one of your variables right away. In
    short: don't ever give a global a name that might be reasonably used by
    a local variable. If this results in large variable names, you can use
    the #define/#undef trick:

    int num_furry_bunny_suits = 2;

    #define suits num_furry_bunny_suits
    void foo(int i) {
    suits += i;
    return suits;
    }
    #undef suits

    Cleaner but (sometimes) less efficient is a local pointer:

    void foo(int i) {
    int* suits = &num_furry_bunny_suits;
    *suits += i;
    return *suits;
    }

    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
     
    Derrick Coetzee, Jan 7, 2005
    #10
  11. wrote:
    >
    > i want to access the value of global variable inside the main function
    > and inside the main func the var with the same name is available..for
    > eg.
    >
    > int x=20;
    >
    > main()
    > {
    > int x=10;
    > printf("%d",x);
    >
    > }
    > i want the o/p to be 20.. how can v achieve that?


    You can't. In fact the above code should be considered a potential
    cause of bugs. If you are lucky enough to be using the GNU C compiler
    (gcc) you can avoid these bugs by compiling with the -Wshadow
    compiler flag. Add -Werror will turn warnings into errors.

    Erik
    --
    +-----------------------------------------------------------+
    Erik de Castro Lopo (Yes it's valid)
    +-----------------------------------------------------------+
    "C++ has its place in the history of programming languages. Just as
    Caligula has his place in the history of the Roman Empire."
    -- Robert Firth
     
    Erik de Castro Lopo, Jan 7, 2005
    #11
  12. writes:
    > i want to access the value of global variable inside the main function
    > and inside the main func the var with the same name is available..for
    > eg.
    >
    > int x=20;
    >
    > main()
    > {
    > int x=10;
    > printf("%d",x);
    >
    > }
    > i want the o/p to be 20.. how can v achieve that?


    The best way to do it is to change the name.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 7, 2005
    #12
  13. sushant

    CBFalconer Guest

    Ben Pfaff wrote:
    > writes:
    >
    >> i want to access the value of global variable inside the main
    >> function and inside the main func the var with the same name is
    >> available..for eg.
    >>
    >> int x=20;
    >>
    >> main()
    >> {
    >> int x=10;
    >> printf("%d",x);
    >> }

    >
    > main()
    > {
    > int x = 10;
    > {
    > extern int x;
    > printf ("%d", x);
    > }
    > ...
    > }


    A foul construct indeed. What happens if the local to main x is
    declared static? I.e. how does the extern actually get resolved.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jan 7, 2005
    #13
  14. sushant

    jacob navia Guest

    Erik de Castro Lopo wrote:
    > wrote:
    >
    >>i want to access the value of global variable inside the main function
    >>and inside the main func the var with the same name is available..for
    >>eg.
    >>
    >>int x=20;
    >>
    >>main()
    >>{
    >>int x=10;
    >>printf("%d",x);
    >>
    >>}
    >>i want the o/p to be 20.. how can v achieve that?

    >
    >
    > You can't. In fact the above code should be considered a potential
    > cause of bugs. If you are lucky enough to be using the GNU C compiler
    > (gcc) you can avoid these bugs by compiling with the -Wshadow
    > compiler flag. Add -Werror will turn warnings into errors.
    >
    > Erik


    lcc-win32 features a -shadows feature too, exactly like the one
    you mention.

    This will be automatically turned on when you ask for a "check"
    pass of the code.
     
    jacob navia, Jan 7, 2005
    #14
  15. sushant

    Ravi Uday Guest

    CBFalconer wrote:
    > Ben Pfaff wrote:
    >
    >> writes:
    >>
    >>
    >>>i want to access the value of global variable inside the main
    >>>function and inside the main func the var with the same name is
    >>>available..for eg.
    >>>
    >>>int x=20;
    >>>
    >>>main()
    >>>{
    >>>int x=10;
    >>>printf("%d",x);
    >>>}

    >>
    >>main()
    >>{
    >> int x = 10;
    >> {
    >> extern int x;
    >> printf ("%d", x);
    >> }
    >> ...
    >>}

    >
    >
    > A foul construct indeed. What happens if the local to main x is
    > declared static? I.e. how does the extern actually get resolved.
    >

    Is the one below at fault ?

    bash-2.02$ cat t17.c
    #include <stdio.h>

    int i = 9;

    int main ()
    {
    static int i = 20;
    {
    extern int i;
    printf ("\nvalue of i decalared outside main = %d\n", i);
    }
    printf ("\nvalue of i decalared in main = %d\n", i);
    return 0;
    }


    bash-2.02$ gcc -Wall -pedantic t17.c
    bash-2.02$ ./a.exe

    value of i decalared outside main = 9

    value of i decalared in main = 20
    bash-2.02$

    - Ravi
     
    Ravi Uday, Jan 7, 2005
    #15
  16. sushant wrote:
    > hi ,
    >
    > can we use scope resolution operator :):) in C???
    >
    > sushant


    I can think of one way you can reference that global variable: Use a
    getter/setter function for each global variable (kludge).

    I've seen code that avoids naming global variables the same as local
    variables by using conventions like g_variable_name for global variables
    and not using any prefix (at least not g_) for any local variables.

    However, this is clearly not ideal. I'd avoid using global variables as
    much as possible. Just my two cents.

    Regards,
    Jonathan.
     
    Jonathan Burd, Jan 7, 2005
    #16
  17. On Fri, 07 Jan 2005 07:36:22 +0000, CBFalconer wrote:

    > Ben Pfaff wrote:


    ....

    >> main()
    >> {
    >> int x = 10;
    >> {
    >> extern int x;
    >> printf ("%d", x);
    >> }
    >> ...
    >> }

    >
    > A foul construct indeed. What happens if the local to main x is
    > declared static? I.e. how does the extern actually get resolved.


    Block scope object identifiers (except declared with extern) have no
    linkage, even static ones. So a block scope static int x; is not a
    candidate for linking to an extern int x;

    If you try to mix internal and external linkage for the same identifier in
    the same translation unit you get undefined behaviour.

    Lawrence
     
    Lawrence Kirby, Jan 10, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. exits funnel

    Scope Resolution Operator

    exits funnel, Dec 12, 2003, in forum: C++
    Replies:
    5
    Views:
    545
    exits funnel
    Dec 13, 2003
  2. richard pickworth

    scope resolution operator

    richard pickworth, Jun 5, 2005, in forum: C++
    Replies:
    3
    Views:
    599
    richard pickworth
    Aug 8, 2005
  3. Jack
    Replies:
    6
    Views:
    552
  4. dwaach
    Replies:
    1
    Views:
    397
    Victor Bazarov
    Jul 6, 2006
  5. heng

    scope resolution operator

    heng, Dec 6, 2006, in forum: C++
    Replies:
    4
    Views:
    379
    Default User
    Dec 6, 2006
Loading...

Share This Page