Problem with Array of Characters

Started by
7 comments, last by Squeejee 22 years, 1 month ago
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
-----
Advertisement
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?
Why make it simple when you can make it sooo nice and complicated?
you forgot index++;
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
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++).
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
While we''re doing string copy routines:
  void my_strcpy(char *dst, const char *src) {  while(*src)    *(dst++) = *(src++);  *dst = ''\0'';}  


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

I gotta thank stroustrup for that one though
Ok, thanks. But can I use:

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

?
-----
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

This topic is closed to new replies.

Advertisement