473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

strange problem

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<windows.h>
  3. #include<string>
  4. using namespace std;
  5.  
  6. LPCSTR lp()
  7. {
  8.     string a="hello";
  9.     LPCSTR b;
  10.     b=a.c_str();
  11.     cout<<"\n in lp : "<<b;
  12.     return b;
  13. }
  14.  
  15. int main()
  16. {
  17.     LPCSTR a;
  18.     a=lp();
  19.     cout<<"\n in main : "<<a;
  20.     return 0;
  21. }
  22.  
OUTPUT:
in lp : hello
in main : ╠╠╠╠╠╠╠╠

why is that in main..its not printing correct value?????
Jun 4 '07 #1
9 1073
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<windows.h>
  3. #include<string>
  4. using namespace std;
  5.  
  6. LPCSTR lp()
  7. {
  8.     string a="hello";
  9.     LPCSTR b;
  10.     b=a.c_str();
  11.     cout<<"\n in lp : "<<b;
  12.     return b;
  13. }
  14.  
  15. int main()
  16. {
  17.     LPCSTR a;
  18.     a=lp();
  19.     cout<<"\n in main : "<<a;
  20.     return 0;
  21. }
  22.  
OUTPUT:
in lp : hello
in main : ╠╠╠╠╠╠╠╠

why is that in main..its not printing correct value?????
Not happening to me,which compiler are u using?
Jun 4 '07 #2
i am using vc++.
i figured out why is it so..
but now i want a way to return that pointer value (without using char *)..
Jun 4 '07 #3
DeMan
1,806 1GB
Which (char *) was that?
Jun 4 '07 #4
Which (char *) was that?
i didnt get your question..
Jun 4 '07 #5
this code with char* instead of lpcstr also does not work.
so how to return the pointer???


#include<iostream>
#include<windows.h>
#include<string>
using namespace std;

const char* lp()
{
string a="hello";
const char* b;
b=a.c_str();
cout<<"\n in lp : "<<b;
return b;
}

int main()
{
const char* a;
a=lp();
cout<<"\n in main : "<<a;
return 0;
}
Jun 4 '07 #6
DeMan
1,806 1GB
Do you get the same problem with :

Expand|Select|Wrap|Line Numbers
  1. const char* lp()
  2. {
  3. char b[] = "hello";
  4. cout<<"\n in lp : "<<b;
  5. return b;
  6. }
  7.  
?
Jun 4 '07 #7
yes..
but i have got the reason why this is happenning..it is so because we are trying to return the pointer to a local variable which is destroyed once the control goes out of the function so..the pointer gives garbage value..
Jun 5 '07 #8
DeMan
1,806 1GB
Great....That's what we were working toward!! (u can malloc the size instead, or use a static array)
Jun 5 '07 #9
Great....That's what we were working toward!! (u can malloc the size instead, or use a static array)
even using malloc inside the local function will give same reuslt..
we can declare the variables globally to overcome this.
Jun 5 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
2
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
12
by: StephQ | last post by:
I have a class Bounds with two constructors: class Bounds { private: list<SegmentupperLinearSpline; // Upper bound. list<SegmentlowerLinearSpline; // Lower bound. ....
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
5
by: ioni | last post by:
Good day, fellows! I have a strange problem – at my site there is a flash strip, that loads data dynamically. It works fine (grabs data from the remote server and presents it), however in IE7...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
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...
0
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...

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.