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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy string into an array of structures

Status
Not open for further replies.

ushtabalakh

Programmer
Jun 4, 2007
132


Code:
struct Word {
  char *data;
  int count;
};

struct Word Words[500];

void main (){
	int distWordCount=0;
	char *s="foo";
	strcpy(Words[distWordCount].data ,s);

}

This code produces the following error
"Unhandled exception at 0x6cb7f6b2 (msvcr80d.dll) in a8.exe: 0xC0000005: Access violation writing location 0x00000000."

And then it shows some assembly file in strcat.asm and one error.

I'm on ms visual c, what's the proper way of copying a string into one element of an array of structures?
 
You need to allocate the space for it first
Code:
   Words[distWordCount].data = malloc (strlen(s) + 1);
   strcpy(Words[distWordCount].data ,s);
 
Worked with

Words[distWordCount].data = (char *) malloc (strlen(s) + 1);

Thanks a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top