problem with the code

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

    problem with the code

    Hi,
    Is there any problem with the following code?

    Regards

    char *func()
    {
    char c;
    return (&c);
    }
    #include<stdio. h>
    int main()
    {
    char *ch;
    ch = func();
    *ch = 'A';
    pritnf("%c",*ch );
    }
  • ramu

    #2
    Re: problem with the code

    On Aug 25, 11:22 am, ramu <ramu....@gmail .comwrote:
    Hi,
    Is there any problem with the following code?
    >
    Regards
    >
    char *func()
    {
    char c;
    return (&c);}
    >
    #include<stdio. h>
    int main()
    {
    char *ch;
    ch = func();
    *ch = 'A';
    pritnf("%c",*ch );
    >
    }
    pritnf("%c",*ch );
    >Sorry. It should be printf("%c", *ch);

    Comment

    • fjblurt@yahoo.com

      #3
      Re: problem with the code

      On Aug 24, 11:22 pm, ramu <ramu....@gmail .comwrote:
      Hi,
      Is there any problem with the following code?
      >
      Regards
      >
      char *func()
      {
      char c;
      return (&c);}
      >
      #include<stdio. h>
      int main()
      {
      char *ch;
      ch = func();
      *ch = 'A';
      pritnf("%c",*ch );
      >
      }
      Looks like homework. How about if you explain whether *you* think
      there's a problem, and why or why not. And don't forget, when you
      submit the assignment, to acknowledge and cite as references the posts
      of those who replied to you.

      Comment

      • Barry Schwarz

        #4
        Re: problem with the code

        On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <ramu.ask@gmail .com>
        wrote:
        >Hi,
        Is there any problem with the following code?
        Yes. It invokes undefined behavior.
        >
        >Regards
        >
        >char *func()
        >{
        char c;
        c is an automatic variable. It comes into existence at the start of
        this block of code.
        return (&c);
        And it goes out of existence when the function returns.
        >}
        >#include<stdio .h>
        >int main()
        >{
        char *ch;
        ch = func();
        At this point ch receives the address of c which has just gone out of
        existence. (By definition, the value in ch becomes indeterminate.)
        *ch = 'A';
        Here you attempt to store the value 'A' into an object that no longer
        exists. (Technically, you are trying to evaluate an indeterminate
        value which invokes undefined behavior.)
        pritnf("%c",*ch );
        This statement also invokes undefined behavior by trying to evaluate
        the address in ch.
        >}
        --
        Remove del for email

        Comment

        • Default User

          #5
          Re: problem with the code

          ramu wrote:
          Hi,
          Is there any problem with the following code?
          >
          Regards
          >
          char *func()
          {
          char c;
          return (&c);
          }
          This returns a pointer to a local variable. That variable goes out of
          scope when the function returns. Any use of the pointer is undefined
          behavior.



          Brian

          Comment

          • jacob navia

            #6
            Re: problem with the code

            Barry Schwarz wrote:
            On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <ramu.ask@gmail .com>
            wrote:
            >
            >Hi,
            > Is there any problem with the following code?
            >
            Yes. It invokes undefined behavior.
            >
            >Regards
            >>
            >char *func()
            >{
            > char c;
            >
            c is an automatic variable. It comes into existence at the start of
            this block of code.
            >
            > return (&c);
            >
            And it goes out of existence when the function returns.
            >
            >}
            >#include<stdio .h>
            >int main()
            >{
            > char *ch;
            > ch = func();
            >
            At this point ch receives the address of c which has just gone out of
            existence. (By definition, the value in ch becomes indeterminate.)
            >
            > *ch = 'A';
            >
            Here you attempt to store the value 'A' into an object that no longer
            exists. (Technically, you are trying to evaluate an indeterminate
            value which invokes undefined behavior.)
            >
            > pritnf("%c",*ch );
            >
            This statement also invokes undefined behavior by trying to evaluate
            the address in ch.
            >
            >}
            >
            FINE, now *you* did *his* homework.

            Isn't clc great?


            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique

            Comment

            • jacob navia

              #7
              Re: problem with the code

              Default User wrote:
              ramu wrote:
              >
              >Hi,
              > Is there any problem with the following code?
              >>
              >Regards
              >>
              >char *func()
              >{
              > char c;
              > return (&c);
              >}
              >
              This returns a pointer to a local variable. That variable goes out of
              scope when the function returns. Any use of the pointer is undefined
              behavior.
              >
              >
              >
              Brian
              Why do you do his homework?

              clc is there to get
              (1): "off topic" remarks for most interesting posts, and
              (2) Do the homework of lazy students.

              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatique

              Comment

              • Richard Heathfield

                #8
                Re: problem with the code

                ramu said:
                Hi,
                Is there any problem with the following code?
                >
                Regards
                >
                char *func()
                {
                char c;
                return (&c);
                }
                #include<stdio. h>
                int main()
                {
                char *ch;
                ch = func();
                *ch = 'A';
                pritnf("%c",*ch );
                }
                Yes, there is a problem with that code (even assuming you meant printf
                rather than pritnf).

                Perhaps you could explain what you're trying to achieve? That would allow
                us to suggest a good way to fix it.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • Richard

                  #9
                  Re: problem with the code

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  ramu said:
                  >
                  >Hi,
                  > Is there any problem with the following code?
                  >>
                  >Regards
                  >>
                  >char *func()
                  >{
                  > char c;
                  > return (&c);
                  >}
                  >#include<stdio .h>
                  >int main()
                  >{
                  > char *ch;
                  > ch = func();
                  > *ch = 'A';
                  > pritnf("%c",*ch );
                  >}
                  >
                  Yes, there is a problem with that code (even assuming you meant printf
                  rather than pritnf).
                  >
                  Perhaps you could explain what you're trying to achieve? That would allow
                  us to suggest a good way to fix it.
                  Why not tell him the returned address of c in func() us meaningless in
                  c.l.c land (it might be useful if you do not dereference it in some sort
                  of diagnostic tool)?

                  Why not tell him that printf() is possibly spelt wrong? Hey maybe he had
                  a function alled "pritnf" elsewhere?

                  And we wont even mention the main return code...

                  Comment

                  • raashid bhatt

                    #10
                    Re: problem with the code

                    On Aug 24, 11:22 pm, ramu <ramu....@gmail .comwrote:
                    Hi,
                          Is there any problem with the following code?
                    >
                    Regards
                    >
                    char *func()
                    {
                       char c;
                       return (&c);}
                    >
                    #include<stdio. h>
                    int main()
                    {
                       char *ch;
                       ch = func();
                       *ch = 'A';
                        pritnf("%c",*ch );
                    >
                    }
                    >
                    >
                    as u see c is a local stack variable as soon a function returns the
                    its stack is cleared and it goes out of scope!

                    Comment

                    • Richard Heathfield

                      #11
                      Re: problem with the code

                      raashid bhatt said:
                      On Aug 24, 11:22 pm, ramu <ramu....@gmail .comwrote:
                      >Hi,
                      >Is there any problem with the following code?
                      >>
                      >Regards
                      >>
                      >char *func()
                      >{
                      >char c;
                      >return (&c);}
                      >>
                      >#include<stdio .h>
                      >int main()
                      >{
                      >char *ch;
                      >ch = func();
                      >*ch = 'A';
                      >pritnf("%c",*c h);
                      >>
                      >}
                      >
                      as u see c is a local stack variable as soon a function returns the
                      its stack is cleared and it goes out of scope!
                      Pretty much, yes. We can reduce the machine-specific nature of the reply by
                      re-wording to something like "c has automatic scope, and so it is
                      destroyed when control returns from func to its caller".

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • jacob navia

                        #12
                        Re: problem with the code

                        Richard Heathfield wrote:
                        raashid bhatt said:
                        >
                        >On Aug 24, 11:22 pm, ramu <ramu....@gmail .comwrote:
                        >>Hi,
                        >>Is there any problem with the following code?
                        >>>
                        >>Regards
                        >>>
                        >>char *func()
                        >>{
                        >>char c;
                        >>return (&c);}
                        >>>
                        >>#include<stdi o.h>
                        >>int main()
                        >>{
                        >>char *ch;
                        >>ch = func();
                        >>*ch = 'A';
                        >>pritnf("%c",* ch);
                        >>>
                        >>}
                        >as u see c is a local stack variable as soon a function returns the
                        >its stack is cleared and it goes out of scope!
                        >
                        Pretty much, yes. We can reduce the machine-specific nature of the reply by
                        re-wording to something like "c has automatic scope, and so it is
                        destroyed when control returns from func to its caller".
                        >
                        I just can't understand why everybody competes in the
                        game:

                        Who does better ramu's homework?

                        You are NOT helping him.

                        --
                        jacob navia
                        jacob at jacob point remcomp point fr
                        logiciels/informatique

                        Comment

                        • pete

                          #13
                          Re: problem with the code

                          Richard Heathfield wrote:
                          raashid bhatt said:
                          >
                          >On Aug 24, 11:22 pm, ramu <ramu....@gmail .comwrote:
                          >>Hi,
                          >>Is there any problem with the following code?
                          >>>
                          >>Regards
                          >>>
                          >>char *func()
                          >>{
                          >>char c;
                          >>return (&c);}
                          >>>
                          >>#include<stdi o.h>
                          >>int main()
                          >>{
                          >>char *ch;
                          >>ch = func();
                          >>*ch = 'A';
                          >>pritnf("%c",* ch);
                          >>>
                          >>}
                          >as u see c is a local stack variable as soon a function returns the
                          >its stack is cleared and it goes out of scope!
                          >
                          Pretty much, yes. We can reduce the machine-specific nature of the reply by
                          re-wording to something like "c has automatic scope, and so it is
                          destroyed when control returns from func to its caller".
                          >
                          ITYM "automatic duration"

                          --
                          pete

                          Comment

                          • Richard Heathfield

                            #14
                            Re: problem with the code

                            pete said:
                            Richard Heathfield wrote:
                            <snip>
                            >Pretty much, yes. We can reduce the machine-specific nature of the reply
                            >by re-wording to something like "c has automatic scope, and so it is
                            >destroyed when control returns from func to its caller".
                            >>
                            >
                            ITYM "automatic duration"
                            SDI. TY.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Richard Heathfield

                              #15
                              Re: problem with the code

                              jacob navia said:

                              <snip>
                              I just can't understand why everybody competes in the
                              game:
                              >
                              Who does better ramu's homework?
                              >
                              You are NOT helping him.
                              Once the cat is out of the bag (which it was, by the time of my second
                              reply), we might as well make sure that it's the right cat.

                              --
                              Richard Heathfield <http://www.cpax.org.uk >
                              Email: -http://www. +rjh@
                              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                              "Usenet is a strange place" - dmr 29 July 1999

                              Comment

                              Working...