|
-
Dec 9th, 2001, 05:44 PM
#1
Thread Starter
Addicted Member
strcat, strcpy..........doesn't work
I can't get strcat and strcpy to work.......see the code below:
Code:
char word[10] = "hello";
char word2[10] = "hi again";
char word3[2];
strcpy(word3, word[0])
strcat(word3, word2[0])
cout << word3;
should output: hh
I've checked the tutorial, but I can't get it to work anyway.......
any ideas?
thanks!
-
Dec 9th, 2001, 05:57 PM
#2
transcendental analytic
that won't work, word[0] and word2[0] are char's not C strings.
you could do
word3[0]=word[0];
word3[1]=word2[0];
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 9th, 2001, 06:06 PM
#3
Thread Starter
Addicted Member
Ok, thanks!
But I wanted to use strcat and strcpy.........and not what you suggested, but thanks anyway
-
Dec 9th, 2001, 06:15 PM
#4
transcendental analytic
strcat and strcpy works only with C strings, if you want to go with C strings then you need a buffer two which you copy the substrings "h" from word and "h" from word2
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 10th, 2001, 10:42 AM
#5
I wonder why people want to use a more complicated approach...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2001, 11:06 AM
#6
Code:
char word[10] = "hello";
char word2[10] = "hi again";
char word3[21];
memset(word3,0x0,sizeof(word3));
strcpy(word3, word);
strcat(word3, word2);
cout << word3;
word3[0=word[0];
word3[1]=word2[0];
word3[2]=0x0;
cout<< word3 << endl;
-
Dec 10th, 2001, 11:09 AM
#7
Originally posted by jim mcnamara
Yeah, this is important!
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2001, 02:10 PM
#8
Thread Starter
Addicted Member
More problemo
problems with arrays and strcat........again!
Code:
char word[10] = "Pointer";
char word2[20] = "Procrastinate";
char word3[2];
word3[0] = word[0];
word3[1] = word2[0];
word3[2] = 'r';
char word4[10] = "problem";
strcat(word3, word4);
cout << word3;
Should output: PPrproblem, but doesn't do that.......any ideas? and why?
thanks again!
-
Dec 10th, 2001, 02:44 PM
#9
Yes. word3 only holds 2 characters.
-
Dec 10th, 2001, 03:46 PM
#10
Thread Starter
Addicted Member
so, Should I change word3[2] to word3[3] and the problems will disappear?
I get a lot of strange characters between the two vectors when I use strcat..........???
-
Dec 10th, 2001, 06:52 PM
#11
-
Dec 10th, 2001, 06:54 PM
#12
use
memset(word3,0x0,sizeof(word3));
C string depend on there being a null terminating character.
In other words for strcat, strcpy and strWHATEVER() to work, there must be an ASCII 0 character at the end of the string.
Start out by setting the whole string to ASCII 0 - what memset does above. It always works.
-
Dec 10th, 2001, 06:58 PM
#13
transcendental analytic
Humm, i don't like this, if you want to stay with C strings then do so but don't mix in char's.
Jim, i think the functions will put the terminating null themself won't they?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 11th, 2001, 06:17 AM
#14
Thread Starter
Addicted Member
thanks - tack
Jim:
Thanks, I think I get it now!
I think I've to read up a little on c-strings, I mix up everything all the time..........
Thanks for helping me and your patience!
-
Dec 11th, 2001, 07:23 AM
#15
Basically a char array must have at least as much space as is needed for all the characters you want to fill in + 1:
word3[0] = word[0];
word3[1] = word2[0];
word3[2] = 'r';
char word4[10] = "problem";
strcat(word3, word4);
You need at least char word3[11] for this.
Keda: strcat will add a '\0' at the end, but after this:
word3[0] = word[0];
word3[1] = word2[0];
word3[2] = 'r';
there is no '\0' at the end, therefore strcat does not know where to put the string.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 11th, 2001, 10:03 AM
#16
Kedaman -
Some string functions don't play fair -- strncpy() for example. It does NOT put a terminating null character.
This is why calloc() exists - it stands for character allocation - it fills allocated memory with nulls.
-
Dec 11th, 2001, 10:07 AM
#17
(after reading CB's post)
Plus, strcpy() is the only function smart enough to place the source string correctly into the destination string when the destination string is full of garbage. strcat(), strncat(), etc must start at a terminating null. Using unitialized strings, then some string functions on top of it-- that is a great way to trash memory.
-
Dec 11th, 2001, 10:54 AM
#18
Thread Starter
Addicted Member
woooorkkkkkkks!
Thanks for your help guys! It works! it works!
thanks for your patience
-
Dec 12th, 2001, 02:22 AM
#19
transcendental analytic
Originally posted by jim mcnamara
Kedaman -
Some string functions don't play fair -- strncpy() for example. It does NOT put a terminating null character.
This is why calloc() exists - it stands for character allocation - it fills allocated memory with nulls.
What a disappointment how about
PHP Code:
inline char* strncpy2(char* to,const char* from, int n){
*(to+n+1)=0;
return strncpy(to,from,n);
};
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|