problem with the code

R

ramu

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);
}
 
R

ramu

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

}
 
F

fjblurt

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

Barry Schwarz

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

Default User

ramu said:
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
 
J

jacob navia

Barry said:
Yes. It invokes undefined behavior.


c is an automatic variable. It comes into existence at the start of
this block of code.


And it goes out of existence when the function returns.


At this point ch receives the address of c which has just gone out of
existence. (By definition, the value in ch becomes indeterminate.)


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


This statement also invokes undefined behavior by trying to evaluate
the address in ch.

FINE, now *you* did *his* homework.

Isn't clc great?
 
J

jacob navia

Default said:
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.
 
R

Richard

Richard Heathfield said:
ramu said:


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

raashid bhatt

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!
 
J

jacob navia

Richard said:
raashid bhatt said:


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

Richard

jacob navia said:
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.
 
A

August Karlstrom

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);
}

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


August
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top