C help!

leezard

Member
May 5, 2002
53
0
0
is the following the same???

void strcat(char s[], char t[])
{

int i,j;
i = 0;
while (s) i++;
j = 0;
while(t[j])
{
s = t[j];
j++;
i++;
}
s=0;
}

________________________________________________

void strcat ( char *sPtr , char *tPtr )
{

int i , j;
i = 0;
while ( *(sPtr + i) ) i++;
j = 0;
while ( *(tPtr + i) )
{
*(sPtr + i) = *(tPtr + i);
j++;
i++;
}
*(sPtr + i) = 0;
}


 

manly

Lifer
Jan 25, 2000
11,685
2,641
136
They look very similar.

But I think the usage of s in the array-based implementation has a bug. Notice you never index into the array, as is done with t.

This duality of arrays and pointers is covered very specifically in K&R. You should check that book to see exactly how it should be done.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
I wrote a huge post then realized his formatting was hosed, so the issues I noted weren't really issues.

Bah...
 

m0ti

Senior member
Jul 6, 2001
975
0
0
nope, they're not the same. in the first one you should almost definitely have *s = t[j], and then s++ (what you have shouldn't work. you don't copy any characters, and you don't move s). also at the end you should do *s = 0, and not s=0 (s is a pointer effectively!).

Actually, now that I think about it, I'm not sure if you even are allowed to move where s points to, since you received the parameter as an array.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Originally posted by: leezard (and modified by stndn)
is the following the same???

void strcat(char s[], char t[])
{

int k,j;
k = 0;
while (s[k]) k++;
j = 0;
while(t[j])
{
s[k] = t[j];
j++;
k++;
}
s[k]=0;
}

________________________________________________

void strcat ( char *sPtr , char *tPtr )
{

int k, j;
k= 0;
while ( *(sPtr + k) ) k++;
j = 0;
while ( *(tPtr + k) )
{
*(sPtr + k) = *(tPtr + k);
j++;
k++;
}
*(sPtr + k) = 0;
}


---- yup, i have too much time on my hand ....
but not too much to make me think of the problem ...


-1066-
 

Chooco

Banned
Apr 5, 2002
731
0
0
what does
void strcat ( char *sPtr , char *tPtr )
even mean? i mean those char *sPtr , char *tPtr
i've seen similar examples in my C++ manuals such as this at the top:
int main(int arg, char* pszArgs[])

what does that mean exactly? the code i saw this from is a basic loop program demonstraighting the looping of -- and ++
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
m0ti you should've check Descartes' and ElDonAntonio's replies, oh and the functions posted are not the same ...

while ( *(tPtr + i) )
{
*(sPtr + i) = *(tPtr + i);
j++;
i++;
}

should been

while ( *(tPtr + j) )
{
*(sPtr + i) = *(tPtr + j);
j++;
i++;
}

and this is how I would've done it

void strcat(char* s, char* t)
{
int i=0, j=0;
for(;*(s+i); i++);
while(*(t+j))
*(s+i++) = *(t+j++);
*(s+i) = 0;
}
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
hmmm, I guess you could also do something like this

void strcat(char* s, char* t)
{
int i=0, j=0;
for(;*(s+i); i++);
do *(s+i++) = *(t+j++); while(*(t+j));
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |