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);
} 18 1520
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);
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.
On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <ra******@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
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
Barry Schwarz wrote:
On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <ra******@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 http://www.cs.virginia.edu/~lcc-win32
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
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
Richard Heathfield <rj*@see.sig.invalidwrites:
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...
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!
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",*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".
--
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
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<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
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<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".
ITYM "automatic duration"
--
pete
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
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
jacob navia wrote:
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<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.
The question is on topic and the answers are subject to review.
You passed up a chance to correct Richard Heathfield.
There is no such thing as "automatic scope".
--
pete
pete said:
jacob navia wrote:
<snip>
>Who does better ramu's homework?
You are NOT helping him.
The question is on topic and the answers are subject to review.
You passed up a chance to correct Richard Heathfield.
And it is now too late, because you beat him to it.
There is no such thing as "automatic scope".
Oh, give it time, give it time...
--
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
jacob navia <ja***@nospam.comwrites:
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<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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kostatus |
last post by:
I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class...
|
by: Keith Dewell |
last post by:
Greetings!
My current job has brought me back to working in C++ which I haven't
used since school days. The solution to my problem may be trivial but
I have struggled with it for the last two...
|
by: harry |
last post by:
Hi,
I have a program that runs on multiple client pc's. Occasionally one or more
of those pc's use VPN to connect to another corporate network. When using
VPN they need to set proxy server in...
|
by: Jon Davis |
last post by:
If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...
BaseClass bc = new ChildClass();...
|
by: Rajat Tandon |
last post by:
Hello there,
I am relatively new to the newsgroups and C#. I have never been disappointed
with the groups and always got the prompt replies to my queries.This is yet
another strange issue, I am...
|
by: Praveen K |
last post by:
I have a problem in communicating between the C# and the Excel Interop
objects. The problem is something as described below.
I use Microsoft Office-XP PIA dll’s as these dll’s were been...
|
by: Ammar |
last post by:
Dear All,
I'm facing a small problem.
I have a portal web site, that contains articles, for each article, the end
user can send a comment about the article.
The problem is:
I the comment length...
|
by: Sarah |
last post by:
I need to access some data on a server. I can access it directly using UNC
(i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network
drive resource (S:\Path\FileName).
Here is my...
|
by: Mike Collins |
last post by:
I cannot get the correct drop down list value from a drop down I have on my
web form. I get the initial value that was loaded in the list.
It was asked by someone else what the autopostback was...
|
by: TPJ |
last post by:
Help me please, because I really don't get it. I think it's some stupid
mistake I make, but I just can't find it. I have been thinking about it
for three days so far and I still haven't found any...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |