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

---- Need help with STRINGS.

Status
Not open for further replies.

DaniDak

Technical User
Joined
Mar 13, 2002
Messages
44
Location
US
Hi,

Can anyone show me how to copy multiple multi-word strings from a linked-list to a dynamiclly sized string array.
I allready have a linked-list with multi-word strings in it, I need to create an array of strings of the exact size needed (dynamically allocated using new), copy the strings to the array from the linked-list.

I know that their is two ways to do it.
1. copy string into a char*[]
2. copy string into a string[]
after I have copied the strings into the array I need to be able to sort and search the array.

thank you
 
The function you want is
strcpy( char *, const char *)
(found in string.h) which copies the chars of the second arguement into the first arguement.

When allocating space for the copy, you can use function
strlen( const char * )
to get the size of a string minus the terminating null character, which sets you up to allocate memory like this:
char * whatever = new char[ strlen( aString ) + 1 ];
then use
strcpy( whatever, aString );
for the copy.
 
you can do it in a loop ..
something similar to this {strcat will do}


#define MAX 200

int main (int argc, char *argv[]) {
int j;
char *buff;
buff = (char*)malloc(MAX);

memset (buff, '\0',sizeof(buff));
for (j=1; j<argc; j++) strcat (buff,argv[j]);

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top