Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with structure

Status
Not open for further replies.

varthi

Programmer
Feb 14, 2002
6
IN
I have small program using structure.The elements in the structure are char arrays.After filling the structure , I am trying to print the values , but values are not printng correctly.

How to solve this problem?

typedef struct
{
char cName[3];
char cAddress[3];
char cLocation[3];
char cCountry[3];

}MSG;

int main(int argc, char* argv[])
{
MSG msg;

strcpy(msg.cName,"Mik");
strcpy(msg.cAddress,"Ray");
strcpy(msg.cLocation ,"LO1");
strcpy(msg.cCountry,"CAN");

cout<<msg.cName<<endl;
cout<<msg.cAddress<<endl;
cout<<msg.cLocation<<endl;
cout<<msg.cCountry<<endl;

return 0;
}


 
Well, a string must have a '\0' as last character to indicate the end. Your string is only 3 chars long,make it 4 and everything should be fine X-)


Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
After what Muppeteer said, i would also do a

memset(msg,0,sizeof(MSG));

JUST IN CASE... I have seen weird things happen without it.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top