473,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 );
}
Aug 25 '08 #1
18 1554
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);
Aug 25 '08 #2
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.
Aug 25 '08 #3
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
Aug 25 '08 #4
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
Aug 25 '08 #5
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
Aug 25 '08 #6
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
Aug 25 '08 #7
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
Aug 25 '08 #8
Richard Heathfield <rj*@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...

Aug 25 '08 #9
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!
Aug 25 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3767
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
7
3783
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 days and would appreciate this group's helpful expertise. My problem may be related to mixing the C and C++ languages together. Namely, I have a struct which I cannot change (as legacy code) similiar to this (I will change the names throughout as...
6
7939
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 Internet Explorer connection settings (proxy:8080). However, as soon as this setting is enabled, the remoting program running on their pc stops communicating with the server it sends data to. I've disabled proxy setting on the affected pc, rebooted...
28
5228
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
9
2827
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 facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
2
5438
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 recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by “System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making the object as null finally....
6
3818
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 is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
8
9766
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 problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
2
4559
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
6
2350
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 solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.