how to call a variable without reference in c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anto frank

    #1

    how to call a variable without reference in c

    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

  • user923005

    #2
    Re: how to call a variable without reference in c

    On Sep 24, 11:19 pm, anto frank <j.antofrank... @gmail.comwrote :
    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.

    Comment

    • Keith Thompson

      #3
      Re: how to call a variable without reference in c

      anto frank <j.antofranklin @gmail.comwrite s:
      [...]
      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_Keit h) kst-u@mib.org <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"

      Comment

      • Richard Bos

        #4
        Re: how to call a variable without reference in c

        Keith Thompson <kst-u@mib.orgwrote:
        anto frank <j.antofranklin @gmail.comwrite s:
        [...]
        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

        Comment

        • karthikbalaguru

          #5
          Re: how to call a variable without reference in c

          On Sep 25, 11:19 am, anto frank <j.antofrank... @gmail.comwrote :
          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




          Comment

          • Ivan Gotovchits

            #6
            Re: how to call a variable without reference in c

            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.

            Comment

            • Keith Thompson

              #7
              Re: how to call a variable without reference in c

              Ivan Gotovchits <ivan.gotovchit s@auriga.ruwrit es:
              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_Keit h) kst-u@mib.org <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"

              Comment

              • Mark McIntyre

                #8
                Re: how to call a variable without reference in c

                On Tue, 25 Sep 2007 06:19:27 -0000, in comp.lang.c , anto frank
                <j.antofranklin @gmail.comwrote :
                >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

                Comment

                Working...