problem with the code

Discussion in 'C Programming' started by ramu, Aug 25, 2008.

  1. ramu

    ramu Guest

    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, Aug 25, 2008
    #1
    1. Advertising

  2. ramu

    ramu Guest

    On Aug 25, 11:22 am, ramu <> wrote:
    > 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);
     
    ramu, Aug 25, 2008
    #2
    1. Advertising

  3. ramu

    Guest

    On Aug 24, 11:22 pm, ramu <> wrote:
    > 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.
     
    , Aug 25, 2008
    #3
  4. On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <>
    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
     
    Barry Schwarz, Aug 25, 2008
    #4
  5. ramu

    Default User Guest

    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
     
    Default User, Aug 25, 2008
    #5
  6. ramu

    jacob navia Guest

    Barry Schwarz wrote:
    > On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <>
    > 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
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Aug 25, 2008
    #6
  7. ramu

    jacob navia Guest

    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
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Aug 25, 2008
    #7
  8. ramu

    Richard Guest

    Richard Heathfield <> writes:

    > 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...
     
    Richard, Aug 25, 2008
    #8
  9. On Aug 24, 11:22 pm, ramu <> wrote:
    > 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!
     
    raashid bhatt, Aug 25, 2008
    #9
  10. ramu

    jacob navia Guest

    Richard Heathfield wrote:
    > raashid bhatt said:
    >
    >> On Aug 24, 11:22 pm, ramu <> wrote:
    >>> 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!

    >
    > 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
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Aug 25, 2008
    #10
  11. ramu

    Richard Guest

    jacob navia <> writes:

    > Richard Heathfield wrote:
    >> raashid bhatt said:
    >>
    >>> On Aug 24, 11:22 pm, ramu <> wrote:
    >>>> 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!

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


    You are assuming it is homework.

    And I hate tell you , but coming out here and finding the right answer
    IS doing ones homework.

    Give the answer and explain it. He wont find it by banging sticks
    together.
     
    Richard, Aug 25, 2008
    #11
  12. ramu wrote:
    > 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);
    > }


    Well, what do gcc with options `-ansi', `-pedantic' and `-Wall' say?


    August
     
    August Karlstrom, Aug 25, 2008
    #12
    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. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,245
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Alan Silver
    Replies:
    1
    Views:
    1,704
    Alan Silver
    Sep 15, 2005
  3. keithb
    Replies:
    1
    Views:
    918
    Bruce Barker
    Mar 29, 2006
  4. Replies:
    0
    Views:
    463
  5. thedarkman
    Replies:
    5
    Views:
    717
    Denis McMahon
    Sep 14, 2010
Loading...

Share This Page