SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 18th, 2003, 06:32 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
trying to ouput: char* pstr

Hi,

Here's what I know: cout will ouput the type char* as a string and not an address like with a normal pointer. In my simple program, I check to make sure I can output the name entered by the user using both the array name and the struct member name. But, when I try to display the name in the output function using the same struct member name, I get weird symbols as the output to the screen. Here is my code:

Code:
#include <iostream>

using namespace std;

struct item
{
	char* pname;
	int number;
};

void read_data(item* pitem)
{
	const int MAX = 20;
	char name[MAX];
	
	
	cout<<"Enter your name: ";
	cin.getline(name, MAX);
	
	cout<<name<<endl;           //successful output
	
	pitem->pname = name;        
	cout<<pitem->pname<<endl;   //successful output

	cout<<"Enter your number: ";
	cin>>pitem->number;
	
	return;
}

void print_data(item* pitem)
{
	cout<<"Here is the name you entered:\n";
	cout<<pitem->pname<<endl;    //****failure: prints out strange symbols

	return;
}

int main()
{
	item a;
	item* pitem=&a;
	
	read_data(pitem);
	
	print_data(pitem);

	return 0;
}

Last edited by 7stud : February 18th, 2003 at 06:45 AM.

Reply With Quote
  #2  
Old February 18th, 2003, 09:12 AM
tony825 tony825 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Greece
Posts: 63 tony825 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
im confused too

#include <iostream>
#define MAX 20

struct item
{
char pname[MAX];
int number;
};


well w/o the pointer int obvious that it works,

i think the problem is trying to "store" a dynamic size for your struct, when re-calling it there's a kind of overflow,

when i run it the first 3 characters were printed corectly and the rest was chineese (not greek since im greek lol).

i tried allocating the pname inside the read section (pitem->pname = new char [pitem->number] ) supposing the number stands for the length of the char but the output was the same.

then i tried this
read
cout<<&(pitem->pname)<<endl;
print
cout<<&(pitem->pname)<<endl;

and saw that there is a dif Ox000020 in HEX wich is 320 b of the 2 pointers.
__________________
No sign

Reply With Quote
  #3  
Old February 18th, 2003, 10:25 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
Quote:
Originally posted by 7stud
Hi,

void read_data(item* pitem)
{
const int MAX = 20;
char name[MAX];


cout<<"Enter your name: ";
cin.getline(name, MAX);

cout<<name<<endl; //successful output

pitem->pname = name;
cout<<pitem->pname<<endl; //successful output

cout<<"Enter your number: ";
cin>>pitem->number;

return;
}

void print_data(item* pitem)
{
cout<<"Here is the name you entered:\n";
cout<<pitem->pname<<endl; //****failure: prints out strange symbols

return;
}

[/code]


The same thing bit me when I was learning C after having been spoiled by Turbo Pascal's handling of strings.

In C/C++, array names are pointers; hence:
char name[20];
char *cp = name; // pointer cp now points to name[0]

Therefore, when you assign one character array to another as in:
pitem->pname = name;
you are making pname point to the string in name. You have not copied the string into pname.

Why it works in read_data() and not in print_data() is because the data, name, is declared locally in read_data() and so still exists. But as soon as you exit read_data(), name goes away. So when you try to output in print_data(), the data no longer exists and pname is pointing to who-knows-what.

Possible solutions:
1. Declare name to be global, so that it will persist between function calls. However, that would make pname superfluous.

2. When you've read in a string for name, dynamically allocate space for it and pointed to by pname, and copy the string from name to pname. Make sure to clean up (ie, delete that allocated space) when you exit the program -- not absolutely necessary in your example, but good practice for avoiding memory leaks in larger projects. Eg:
Code:
cout<<name<<endl;           //successful output
pitem->pname = new char[strlen(name)+1];        
strcpy(pitem->pname,name); // now pname has copy of string


I'm sure that Solution #2 is what you are looking for.


PS
Space for local variables in a function is allocated on the stack as you enter the function and is removed from the stack when you leave.

Reply With Quote
  #4  
Old February 18th, 2003, 03:01 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Quote:
The same thing bit me when I was learning C ....In C/C++, array names are pointers;


I've learned that lesson. The scope is what bit me i.e. when the first function terminates what pname pointed to no longer exists. Thanks a lot for taking the time to read my code.

Last edited by 7stud : February 18th, 2003 at 03:37 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > trying to ouput: char* pstr


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway