February 18th, 2003, 06:32 AM
-
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.
February 18th, 2003, 09:12 AM
-
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
February 18th, 2003, 10:25 AM
-
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.
February 18th, 2003, 03:01 PM
-
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.