C/C++ >> Question on the string function
Posted by SilentStrike on 01:58:00 06-01-2001

void StrCopy(char *src, char *dest)
{
while(*src)
{
*dest=*src;
src++;dest++;
}
}

What does the

while (*src)

line of mean?

I assume it means that the value pointed by source does not equal '\0'. I guess that would go along with the whole while (variable) thing means while the variable is not 0, but I never really thought of the '\0' as a zero.

Posted by fabs on 06:21:00 06-01-2001
yeah \0 is NULL and that's 0 and that is false
So if you code something like this...
while(*ptr)
{
/*Code*/
}

... it will loop until it reaches the terminating \0.
fabs
Posted by fabs on 06:21:00 06-01-2001
yeah \0 is NULL and that's 0 and that is false
So if you code something like this...
while(*ptr)
{
/*Code*/
}

... it will loop until it reaches the terminating \0.
fabs
Posted by KaGez on 14:35:00 06-03-2001
OK , then let's just rewrite it into a more simple function =) Here we go :

void StrCopy(char *src, char *dest)
{
while(*src != 0)
{
*dest=*src;
src++;
dest++;
}
}

Easy enuff ??

KaGez
Posted by justin on 03:55:00 06-04-2001
well, some people dislike seeing while(somevar != 0)
because
while(somevar)
is nicer, simpler, and phsycologically faster, and easier to type...

[ This Message was edited by: justin on 2001-06-04 03:55 ]
Posted by fabs on 07:09:00 06-04-2001
I'd avoid it, too. It just looks messy.
fabs
Posted by KaGez on 10:01:00 06-04-2001
It was just to explain it ... I'd also don't use it
Anyways , it was just for explanation ....
Posted by robost86 on 10:31:00 06-04-2001
L0:
LODSB ; LOAD BYTE FROM DS:SI TO AL, AND INCREASE SI BY 1
STOSB ; STORE BYTE FROM AL TO ES:DI, END INCREASE DI BY 1
OR AL,AL ; SET ZF OF AL = 0
JNZ L0 ; IF AL != 0 COPY NEXT BYTE

This is how it's done in assembler. 6 bytes, probably the smallest way to do it. But the only reason to program in high level languages is that you want the programs a bit more structured. Changing the address of a pointer is nothing I recommend, use an index variable instead.
Posted by fabs on 11:28:00 06-04-2001
I strongly disagree: Incrementing a pointer in a loop looks much more structured then using an index-value. what you might wanna do however, is treat the pointer like an array. For example this worx:
int a[19];

int *b= a; //Assing &a[0] to b.
b[3]=4;

So b[3] can be used as an equivalent for a[3]. This is extremly useful when passing pointers to functions.

fabs
Posted by robost86 on 13:12:00 06-04-2001
Hmm.. Why not use a[3] directly when passing pointer to a function? There is no need of using:

int* b;
b = a;
b[3] = something;
Posted by robost86 on 13:20:00 06-04-2001
Sorry, fabs. What was I thinking about? . Incrementing the pointer is faster and as structured, but using index variables can also be useful in some situations, like this:

int i;
char* s;
while(s[i]!=0) {
printf("%d: %c\n",i,s);
}
Posted by KaGez on 14:02:00 06-05-2001
lol
I think this is goin faaaaaarrr away from SilentStikes post ... hehe
[addsig]