Click to See Complete Forum and Search --> : Append Array with Pointer
Daok
Oct 31st, 2002, 09:15 PM
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?
#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
Daok
Nov 1st, 2002, 08:23 AM
I'm happy to found that is not a easy problem....
Morphine
Nov 1st, 2002, 11:07 AM
try this:
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....
parksie
Nov 1st, 2002, 11:28 AM
main() {int main(void) {
;)
Daok
Nov 1st, 2002, 01:00 PM
The idea with my question was how to make it with pointer without any stuff already done like strcat.
jim mcnamara
Nov 1st, 2002, 02:09 PM
How about -
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;
Daok
Nov 1st, 2002, 03:42 PM
Why register???
Daok
Nov 1st, 2002, 04:01 PM
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?
jim mcnamara
Nov 1st, 2002, 04:34 PM
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:
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;
}
twanvl
Nov 1st, 2002, 04:57 PM
You can't change what an array that is declared as "char name[size];" points to. I suggest that you use C++ strings:
#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
parksie
Nov 1st, 2002, 05:02 PM
In that case, what's the point of another function call?
kedaman
Nov 2nd, 2002, 06:21 AM
habit of using functions?
parksie
Nov 2nd, 2002, 07:19 AM
Quiet, you ;)
How's Sq?
kedaman
Nov 2nd, 2002, 07:43 AM
hehe;)
the compiler is still being planned, the language definitions are quite set now i think.
CornedBee
Nov 4th, 2002, 11:35 AM
char *ptrDebut = new char;
Once I'm away everyone goes blind...
A single character is not enough to hold two complete arrays.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.