Archived

This topic is now archived and is closed to further replies.

Problem with Array of Characters

This topic is 5774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

If I got this: struct structure { char text[50]; }; structure.text = "Blah blah and blah."; char words[50]; How do I make 'words' equal to 'structure.text'? I have tried: words = structure.text; but that would not compile. I also tried: words[50] = structure.text[50]; but that just made words equal to a bunch of jibberish. How can I do this? Edited by - Squeejee on February 19, 2002 9:02:52 PM Edited by - Squeejee on February 19, 2002 9:04:16 PM

Share on other sites
I usually use the STL string class but I think you can use

strcpy(text,words) or:

  void CopyArray(char cstr1[], char cstr2[]){ int index =0; do { cstr2[index]=cstr1[index]; }while(cstr1[index]!=''\0'');}

Good luck!

Why make it simple when you can make it sooo nice and complicated?

Share on other sites
you forgot index++;

Share on other sites
Since you''re dealing with C-style strings, try using strcpy. It''s in string.h (cstring would work just as well if you''re using C++).

Share on other sites
quote:
Original post by clabinsky

(edited by pirate_dau (yea, I''m feelin kinda worthless))

  void CopyArray(char cstr1[], char cstr2[]){ int index =0; do { cstr2[index]=cstr1[index]; }while(cstr1[index++]!=''\0'');}

So if you''d still rather use an array of characters, you actually have to copy each char at a time as this code does (since arrays can be thought of as just a pointer to the first element of the array, and if you do a straight assignment, typically it is a "bad thing(tm)" since what it''s REALLY doing is assigning a pointer to the same data) This is also why this function doesn''t return anything, but still does change the data. Also, you should make an effort to not to step over the array bounds, but so long as a ''\0'' exists in the string it will work this way. If you aren''t sure of a ''\0'' existing, well then you could get into trouble.

This explanation is probably unnecessary, but what else do I have to do with my time? Heh

-pirate_dau

Share on other sites
While we''re doing string copy routines:
  void my_strcpy(char *dst, const char *src) { while(*src) *(dst++) = *(src++); *dst = ''\0'';}

Share on other sites
Here''s mine:
void strcpy(char *dst,char *src){while(*dst++=*src++);}

I gotta thank stroustrup for that one though

Share on other sites
Ok, thanks. But can I use:

if(text==structure.word)
// do code

?

Share on other sites
quote:
Original post by Squeejee
Ok, thanks. But can I use:

if(text==structure.word)
// do code

?

Nope. You''ll have to use a comparison function. Check the docs on strcmp(), for example. That equivalence statement you posted is not comparing one string to another -- it''s comparing one pointer to another which is likely not what you''re going for.

-pirate_dau

• Forum Statistics

• Total Topics
628674
• Total Posts
2984159

• 13
• 12
• 10
• 9
• 9