|
-
Apr 14th, 2002, 05:50 PM
#1
Thread Starter
Lively Member
adding stuff to the beginning of arrays?
Let's say I have a char array, okay?
I initialize it:
char gerbil[255] = "mike tyson";
But let's say that, later in the program, I want to add the following string to the beginning of it:
"that stupid ear-biting "
so this would be true:
gerbil == "that stupid ear-biting mike tyson"
any answers?
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Apr 14th, 2002, 10:49 PM
#2
Lively Member
create a new string:
char hamster[] = "that stupid ear-biting " ;
then concatanate the two:
char hamger[100];
hamger = strcat(hamster, gerbil);
ps - you gotta use <string.h> for this
-
Apr 14th, 2002, 11:51 PM
#3
Thread Starter
Lively Member
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Apr 15th, 2002, 01:06 PM
#4
that won't work.
it will append the content of gerbil to the buffer of hamster (which is too small) and then assign the memory address of hamster to hamger (probably a syntax error). The correct way would be:
Code:
// create a new string:
char hamster[100] = "that stupid ear-biting " ;
// then concatanate the two:
strcat(hamster, gerbil);
Now hamster contains the complete string. You must make sure that hamster is large enough to hold both.
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.
-
Apr 15th, 2002, 01:23 PM
#5
Lively Member
yep i was wrong about the assignment but you can definately declare an array dynamically and then still strcat onto it.
-
Apr 15th, 2002, 02:49 PM
#6
yeah, but your code didn't allocate an array dynamically...
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.
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
|