Often in the written examination and interview, the company will have some C language library function for the interviewer to do, these functions look very simple, in fact, still have to consider a lot. Here are some simple implementations of several commonly used functions.
1. String copy function
String assignment function
char *strcpy (char *strdestination, const char *strsource)
{
//
assert (strdestination!= Null && strsource!= null);
int i = 0;
while (*strsource!= ' ")
{
strdestination[i++] = *strsource++;
}
Strdestination[i] = ' the ';
return strdestination;
}
2. Memory copy function
void *memcpy (void *dest, const void *SRC, size_t count)
{
assert (dest!= null && src!= null);
Char *tmp = (char*) dest;
const char *s = (const char*) src;
while (count--)
{
*tmp = *s;
}
return dest;
}
3. Memory Setup function
void *memset1 (void *dest, int c, size_t count)
{
assert (dest!= NULL);
char* tmp = (char*) dest;
int i = 0;
while (count-)
{
tmp[i++] = c;
}
return dest;
}
4. Converts a string to an integer
int atoi (const char *string)
{
char *p = (char*) string;
char c;
int i = 0;
while (c = *p++)
{
if (c>= ' 0 ' && c<= ' 9 ')
{
i = i*10 + (c ' 0 ');
}
else
return-1; Invalid string
} return
i;
}
To achieve so much first.