|
-
Oct 31st, 2002, 10:15 PM
#1
Thread Starter
Ya ya Baby!!!Me is Back
Append Array with Pointer
Hello,
I have a question. I am trying to code a function that will append two array.
Here is the code, all work but the value doesn't return well, why?
Code:
#include <iostream>
using namespace std;
void CopyChaineSuite(char *&s1, char *s2);
void main()
{
char cPrenom1[80] = "Me";
char cPrenom2[80] = "andYou";
CopyChaineSuite(cPrenom1, cPrenom2);
cout << "Prenom #1 : " << cPrenom1 << endl;
cout << "Prenom #2 : " << cPrenom2 << endl;
}//Fin du main
void CopyChaineSuite(char *&s1, char *s2)
{
char *ptrDebut = new char;
char *p = ptrDebut;
while(*s1 != '\0')
*ptrDebut++ = *s1++;
while(*s2 != '\0')
*ptrDebut++ = *s2++;
*ptrDebut = '\0';
s1 = p;
}//Fin de CopyChaineSuite
-
Nov 1st, 2002, 09:23 AM
#2
Thread Starter
Ya ya Baby!!!Me is Back
I'm happy to found that is not a easy problem....
-
Nov 1st, 2002, 12:07 PM
#3
New Member
try this:
Code:
main() {
char *firstString, *secondString;
firstString = (char *)malloc( 80 ); //or however many bytes you want
secondString = (char *)malloc( 80);
firstString = "something";
secondString = " else";
strcat( firstString, secondString );
/***********
or even
strncat( firstString, secondString, max_bytes_to_append );
**********/
//and then
free( firstString );
free( secondString );
//when you are done with them....
-
Nov 1st, 2002, 12:28 PM
#4
Monday Morning Lunatic
int main(void) {
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 1st, 2002, 02:00 PM
#5
Thread Starter
Ya ya Baby!!!Me is Back
The idea with my question was how to make it with pointer without any stuff already done like strcat.
-
Nov 1st, 2002, 03:09 PM
#6
Frenzied Member
How about -
Code:
char *cat(char *dest,char *src){
register char *buf,*s;
for(buf=dest;*buf;buf++); /* goto end of current array */
for(s=src;*s;buf++,s++) *buf=*s; /* add characters */
*buf=0x00; /* null terminate string */
return dest;
-
Nov 1st, 2002, 04:42 PM
#7
Thread Starter
Ya ya Baby!!!Me is Back
-
Nov 1st, 2002, 05:01 PM
#8
Thread Starter
Ya ya Baby!!!Me is Back
ok
Code:
void CopyChaineSuite(char *s1, char *s2)
{
while(*s1 != '\0'){s1++;}
while(*s2 != '\0'){*s1++ = *s2++;}
*s1 = '\0';
}//Fin de CopyChaineSuite
This is my code and yours together and it work well.
I have an other question, can I know if it's possible to do s1--... if I want to make a reverser function I will need it right?
-
Nov 1st, 2002, 05:34 PM
#9
Frenzied Member
Yes you can go backwards with a pointer. One caveat -
Don't go back before the start of the buffer or you will have some nasty surprises Try something like ptrdiff() below:
Code:
char *strrev(char *s){ // reverse a string
register char *buf,*dest;
char *tmp;
if(!*s) return s;
for(buf=s;*buf;buf++);
tmp=calloc(1,ptrdiff(buf,s);
buf--;
for(dest=tmp; ptrdiff(buf,s);buf--,dest++) *dest=*buf;
*dest=0x00;
cpy(s,tmp);
return s;
}
void cpy(char *dest,char *src){
register char *buf, *s;
for(s=src,buf=dest;*s;buf++,s++)*buf=*s;
buf=0x00;
return;
}
int ptrdif(void *a, void *b){
return (int)a-(int)b;
}
-
Nov 1st, 2002, 05:57 PM
#10
You can't change what an array that is declared as "char name[size];" points to. I suggest that you use C++ strings:
Code:
#include <iostream>
#include <string>
using namespace std;
void CopyChaineSuite(char *&s1, char *s2);
int main()
{
string cPrenom1 = "Me";
string cPrenom2 = "andYou";
CopyChaineSuite(cPrenom1, cPrenom2);
cout << "Prenom #1 : " << cPrenom1 << endl;
cout << "Prenom #2 : " << cPrenom2 << endl;
return 0;
}//Fin du main
void CopyChaineSuite(string &s1, const string& s2)
{
s1 += s2;
}//Fin de CopyChaineSuite
-
Nov 1st, 2002, 06:02 PM
#11
Monday Morning Lunatic
In that case, what's the point of another function call?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 2nd, 2002, 07:21 AM
#12
transcendental analytic
habit of using functions?
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.
-
Nov 2nd, 2002, 08:19 AM
#13
Monday Morning Lunatic
Quiet, you 
How's Sq?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 2nd, 2002, 08:43 AM
#14
transcendental analytic
hehe
the compiler is still being planned, the language definitions are quite set now i think.
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.
-
Nov 4th, 2002, 12:35 PM
#15
char *ptrDebut = new char;
Once I'm away everyone goes blind...
A single character is not enough to hold two complete arrays.
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
|