how to call a variable without reference in c

Discussion in 'C Programming' started by anto frank, Sep 25, 2007.

  1. anto frank

    anto frank Guest

    hi friends,

    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

    IS IT POSSIBLE?

    please help me
     
    anto frank, Sep 25, 2007
    #1
    1. Advertising

  2. anto frank

    user923005 Guest

    On Sep 24, 11:19 pm, anto frank <> wrote:
    > hi friends,
    >
    > 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()


    The main() function returns int.

    > {
    > int a=10;
    > fun();
    > }
    > fun( )
    > {
    > printf("%d", a);
    >
    > }
    >
    > condition
    > 1. don't use pointer concept


    It's going to be pretty hard to print without any pointers.

    > 2.don't declare as global variable
    > 3.don't send as arguments
    >
    > IS IT POSSIBLE?


    All things are possible except those that aren't.

    For instance, write a file in function 1.
    Read the file from function 2.
     
    user923005, Sep 25, 2007
    #2
    1. Advertising

  3. anto frank wrote:
    > hi friends,
    >
    > 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?


    You should start by learning the very elementary, most trivial parts of
    the language before playing games with trying to circumvent the
    mechanism for passing (not "calling") arguments to functions. For example:
    >
    > void main()

    ^^^^
    This marks you immediately as ignorant. Now, ignorance is no sin: we
    all are ignorant of many things, and this deficiency can be easily
    overcome. In this case, turn to the very first pages of any textbook on
    C written by even a semi-competent C programmer. main returns an int.

    > {
    > int a=10;
    > fun();


    main returns an int; you ought to have it actually do so.
    > }
    > fun( )
    > {
    > printf("%d", a);

    ^^^^^^
    printf is variadic and must have a declaration visible. You have none.
    The simplest way to provide it is with
    #include <stdio.h>
    And 'a' in main() has scope only of the block in which it is declared.
    The 'a' in fun() has no declaration and your compiler ought to have told
    you so.
    Implicit int has been disallowed for eight years now. If fun returns an
    int, you should say so.
    > }
    >
    > condition
    > 1. don't use pointer concept

    Then you can't even call printf, since its first argument is a pointer.

    > 2.don't declare as global variable
    > 3.don't send as arguments
    >
    > IS IT POSSIBLE?
    >
    > please help me


    Why should you want to do such a worthless thing?
     
    Martin Ambuhl, Sep 25, 2007
    #3
  4. anto frank <> writes:
    [...]
    > condition
    > 1. don't use pointer concept
    > 2.don't declare as global variable
    > 3.don't send as arguments


    4. Do your own homework.

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 25, 2007
    #4
  5. anto frank

    Richard Bos Guest

    Keith Thompson <> wrote:

    > anto frank <> writes:
    > [...]
    > > condition
    > > 1. don't use pointer concept
    > > 2.don't declare as global variable
    > > 3.don't send as arguments

    >
    > 4. Do your own homework.


    5. And while you're at it, kick your teacher. It's a stupid question.

    Richard
     
    Richard Bos, Sep 25, 2007
    #5
  6. On Sep 25, 11:19 am, anto frank <> wrote:
    > hi friends,
    >
    > 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
    >
    > IS IT POSSIBLE?
    >
    > please help me


    It is Funny, But Some ways are there :):):)
    1)Write a into an external memory in main before calling fun and read
    from the external memory in fun before printing it.
    2)Do a bit of assembly and push it in memory stack explicitly and pop
    it out explicitly using assembly in the fun.
    3)Copy to free register of the CPU and read it out from there.

    Karthik Balaguru
     
    karthikbalaguru, Sep 25, 2007
    #6
  7. anto frank

    Richard Guest

    Martin Ambuhl <> writes:

    > anto frank wrote:
    >> hi friends,
    >>
    >> 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?

    >
    > You should start by learning the very elementary, most trivial parts
    > of the language before playing games with trying to circumvent the
    > mechanism for passing (not "calling") arguments to functions. For
    > example:
    >>
    >> void main()

    > ^^^^
    > This marks you immediately as ignorant. Now, ignorance is no sin: we


    No. This marks him as uninformed. We can play word games, but "ignorant"
    is rude and offensive. Blame the course work. Of course, it could be a
    troll too. It has all the marks of being so bad while appealing to
    almost anyone to correct since it's just more of the sameo, sameo basic
    errors. Lets tick off the usual suspects:

    1) Appears to be homework : check
    2) Incorrect main() declaration : check
    3) lack of standard include : check
    4) Use of the word "friends" : check
    5) spelling of function as fuction : check
    6) mention of global variables : check

    Troll % chance : 89.38784%

    > all are ignorant of many things, and this deficiency can be easily
    > overcome. In this case, turn to the very first pages of any textbook
    > on C written by even a semi-competent C programmer. main returns an
    > int.
    >
    >> {
    >> int a=10;
    >> fun();

    >
    > main returns an int; you ought to have it actually do so.


    Not in his declaration it didn't. Main *should* return an int. Different
    thing.


    >> }
    >> fun( )
    >> {
    >> printf("%d", a);

    > ^^^^^^
    > printf is variadic and must have a declaration visible. You have
    > none. The simplest way to provide it is with
    > #include <stdio.h>
    > And 'a' in main() has scope only of the block in which it is
    > declared. The 'a' in fun() has no declaration and your compiler ought
    > to have told you so.
    > Implicit int has been disallowed for eight years now. If fun returns
    > an int, you should say so.
    >> }
    >>
    >> condition
    >> 1. don't use pointer concept

    > Then you can't even call printf, since its first argument is a pointer.
    >
    >> 2.don't declare as global variable
    >> 3.don't send as arguments
    >>
    >> IS IT POSSIBLE?
    >>
    >> please help me

    >
    > Why should you want to do such a worthless thing?


    Homework I would guess. Or a troll.
     
    Richard, Sep 25, 2007
    #7
  8. anto frank wrote:

    > hi friends,
    >
    > 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
    >
    > IS IT POSSIBLE?
    >
    > please help me

    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'.
    --
    s/.../.gotovchits/g for email.
     
    Ivan Gotovchits, Sep 25, 2007
    #8
  9. anto frank

    Flash Gordon Guest

    Ivan Gotovchits wrote, On 25/09/07 16:29:
    > anto frank wrote:
    >
    >> hi friends,
    >>
    >> 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
    >>
    >> IS IT POSSIBLE?
    >>
    >> please help me

    > 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).


    It might on your implementation, or it might not. The compiler could
    even not bother allocating any memory for a.

    <snip>

    > int *p;


    One requirement was to no use pointers.

    > p -= 2; /* a return address and p itself (8 bytes total) */


    p is unititialised and so could contain anything.

    > 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'.


    Your suggested code is almost certain to fail for many reasons.
    --
    Flash Gordon
     
    Flash Gordon, Sep 25, 2007
    #9
  10. Ivan Gotovchits <> writes:
    > 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) <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"
     
    Keith Thompson, Sep 25, 2007
    #10
  11. On Tue, 25 Sep 2007 06:19:27 -0000, in comp.lang.c , anto frank
    <> wrote:

    >hi friends,
    >
    > is it possible to call a variable from the main function to a sub

    ....
    >condition
    >1. don't use pointer concept
    >2.don't declare as global variable
    >3.don't send as arguments


    "Is it possible to eat sausages ?
    Condition:
    1. don't kill pig
    2. don't construct sausage
    3. don't send to mouth"

    :)

    To answer your question - yes, sure. Just write the data to a file,
    then read it in again. Ah, no - this needs a file pointer. Oh well.

    If this is homework, your teacher needs upgrading....
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Sep 25, 2007
    #11
  12. In article <>,
    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?


    We get this question posed to us every few months; you could probably
    find old discussions of it via google groups.

    If I recall correctly (and I did not pay close attention), the
    answer being looked for by the people who framed the question, was
    to declare a local array in the subfunction and then to index it
    beyond the end of the array in order to gain access to data on
    a stack. Which end of the array you have to run off depends on
    the details of the architecture, but the whoever wrote the
    question assumed that everyone used the same architecture and
    assumed that stacks were present and assumed that running off
    the end of the array would work, and probably various other
    poor assumptions too.
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
     
    Walter Roberson, Sep 25, 2007
    #12
  13. anto frank

    user923005 Guest

    On Sep 25, 12:32 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    > 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?

    >
    > We get this question posed to us every few months; you could probably
    > find old discussions of it via google groups.
    >
    > If I recall correctly (and I did not pay close attention), the
    > answer being looked for by the people who framed the question, was
    > to declare a local array in the subfunction and then to index it
    > beyond the end of the array in order to gain access to data on
    > a stack. Which end of the array you have to run off depends on
    > the details of the architecture, but the whoever wrote the
    > question assumed that everyone used the same architecture and
    > assumed that stacks were present and assumed that running off
    > the end of the array would work, and probably various other
    > poor assumptions too.


    Of course that is nothing more than undefined behavior.

    I guess that according to the original formulation you could use a
    static variable (after all, it only has file scope and not global
    scope).
     
    user923005, Sep 25, 2007
    #13
  14. anto frank

    Richard Bos Guest

    -cnrc.gc.ca (Walter Roberson) wrote:

    > In article <>,
    > 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?

    >
    > We get this question posed to us every few months; you could probably
    > find old discussions of it via google groups.
    >
    > If I recall correctly (and I did not pay close attention), the
    > answer being looked for by the people who framed the question, was
    > to declare a local array in the subfunction and then to index it
    > beyond the end of the array in order to gain access to data on
    > a stack. Which end of the array you have to run off depends on
    > the details of the architecture, but the whoever wrote the
    > question assumed that everyone used the same architecture and
    > assumed that stacks were present and assumed that running off
    > the end of the array would work, and probably various other
    > poor assumptions too.


    And didn't understand that running off the end of an array is the same
    thing as using a wild pointer, so this solution actually doesn't conform
    to the demands. But then, you have to understand C to realise that, and
    the OP's teacher plainly doesn't.

    Richard
     
    Richard Bos, Sep 27, 2007
    #14
    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. Replies:
    5
    Views:
    375
  2. ravi
    Replies:
    10
    Views:
    1,170
    Chris Torek
    Feb 23, 2007
  3. Ant Franck

    how to use a variable without reference

    Ant Franck, Oct 16, 2007, in forum: C Programming
    Replies:
    9
    Views:
    353
    Kenneth Brody
    Oct 17, 2007
  4. Saeed Amrollahi

    Call by value vs. Call by reference

    Saeed Amrollahi, Jul 11, 2008, in forum: C++
    Replies:
    10
    Views:
    644
    KalleGuld
    Jul 15, 2008
  5. James Marshall
    Replies:
    2
    Views:
    98
    James Marshall
    Feb 28, 2005
Loading...

Share This Page