Thread: cstring in C++
View Single Post
Old Apr 11, 2005, 01:35 PM   #1 (permalink)
blix9
DriverHeaven Addict
 
Join Date: Jun 2002
Posts: 315
blix9 is on a distinguished road

cstring in C++

I hope this is a right place to ask something like this.
What is with this c-string thing? I know they are basically a pointer to the first elements of the char array. I thought I wrote everything correct regarding the rules of the null-terminated char arrays in my last homework. However, when I pass them around in some 'getName()' and 'printName()' member functions in a class and try to output in a main function they get kinda all mangled up and display garbage with partially correct outputs in the begining part of the names... such as...

Julliane M%$@&* // Julliane Moore
Tom @#^ //Tom Cruise



The thing is, to print a name, I have to pass a char* around as a paprameter in some helper functions in the class which are not visible to clients. I just am required to do this way. Later, also, I have to use the same helper function(display) for non-member variable which is passed on by a member functions.

class personalInfo
{
personalInfo(char* name, int age ......);
....
....
char* getName() const;
....
};


void display(char*); // helper function prototype in class.cpp file

void printName() const // in class.cpp file
{ display( getName() ); }

// in test code it looks like

aClass.printName(); //in testClass.cpp file

Can you think of what's causing this? I think am doing something stupid with this. Maybe it is not a c-string problem? I am clueless.
Any help would be appreciated. BTW, I am using gcc compiler in a school server, not MS Visual C++. It has to work in school machines.
If anyone is interested I can present the code for the class and test file here.

thanks...

Last edited by blix9; Apr 11, 2005 at 04:36 PM.
blix9 is offline   Reply With Quote