Based on the C language string function of some use experience

  • 2020-04-02 01:28:59
  • OfStack

Take strcat as an example of a string splicing function.
Prototype: Extern char *strcat(char *dest,char * SRC);
Usage: # include < String. H >
Function: Add the SRC reference string to the end of dest (overwriting the '\0' at the end of dest) and add '\0'.
Description: The memory area indicated by SRC and dest must not overlap and dest must have enough space to hold the SRC string.
          Returns a pointer to dest.
For example:


// strcat.c
      #include <syslib.h>
      #include <string.h> 
      main()
      {
        char d[20]="Golden Global";
        char *s=" View";
        clrscr();
        strcat(d,s);
        printf("%s",d); 
        getchar();
        return 0;
      }

The code above can output Golden Global View without problems.
But here's how:

    // strcat.c
      #include <syslib.h>
      #include <string.h> 
      main()
      {
        char *p="Golden Global";
        char *s=" View";
        clrscr();
        strcat(p,s);
        printf("%s",p); 
        getchar();
        return 0;
      }

Parameter is the two pointer parameters that meet its requirements, but the program does not run. I began to wonder why the parameters were of the right type but could not return the desired result. So you can only look at the function prototype.

Strcat function prototype


char *strcat(char *strDest, const char *strScr) //Const the source string to indicate that it is an input parameter
{
    char * address = strDest;             //This statement will compile incorrectly if placed after assert

    assert((strDest != NULL) && (strScr != NULL)); //Add a non-0 assertion to the source and destination addresses

    while(*strDest)             //Is the while (* strDest! ='0')
    {                        //If you use while(*strDest++), you get an error because ++ is not cycled
        strDest++;               //Constraints. So it's going to be ++ in the circulation; Because if *strDest is last
    }                        //Marks the end of the string '0'.
    while(*strDest++ = *strScr++) //Is the while (strScr++ strDest++ = (* *)! ='0')
    {
        NULL;                 //You can use ++ in this loop,
    }                          //StrDest ='0'; Is it necessary?

    return address;               //To implement the chain operation, the destination address is returned
}

From this sentence we know why

while(*strDest++ = *strScr++)
{
    NULL;
}

If strDest is a pointer, the *strDest here takes a value of an unknown address, which the compiler will not tolerate. But why is it ok when strDest is an array, because an array is equal to assigning it a contiguous address. Of course, the security address can be used. Of course, we can also write a real string concatenation function with pointer as parameter. The following is a function prototype written by me:

char *strcatDemo2(char *str1, const char *str2) //Const the source string to indicate that it is an input parameter
{
    assert((str1 != NULL) &&(str2 != NULL));

    char *address = (char *)malloc((strlen(str1) + strlen(str2) + 1) *sizeof(char));

    char *des = address;

    assert(address != NULL);

    while(*str1)
    {
        *address = *str1;
        str1++;
        address++;
    }

    while(*str2)
    {
        *address = *str2;
        str2++;
        address++;
    }    

    *address = '0';

    return des;
}

In this case, address is given space to hold two strings. Note that you need to apply for one more because the string requires a '\0' ending. Use is used like this:

int main(int argc, char *argv[])
{
    char *p = "hello, ", *s = "world!";

    char *t = strcatDemo2(p, s);
    puts(t);

  system("PAUSE");    
  return 0;
}

The one written above is similar to the function of adding strings in C#.
In fact, most strings in C are used as an array of characters and a pointer to a character. Here are the prototypes to take a good look at and avoid future mistakes.

Strcat function prototype:


char *strcat(char *strDest, const char *strScr) //Const the source string to indicate that it is an input parameter
{
       char * address = strDest;             //This statement will compile incorrectly if placed after assert
       assert((strDest != NULL) && (strScr != NULL)); //Add a non-0 assertion to the source and destination addresses
       while(*strDest)             //Is the while (* strDest! ='0')
       {                        //If you use while(*strDest++), you get an error because ++ is not cycled
              strDest++;               //Constraints. So it's going to be ++ in the circulation; Because if *strDest is last
       }                        //Marks the end of the string '0'.
       while(*strDest++ = *strScr++) //Is the while (strScr++ strDest++ = (* *)! ='0')
       {
              NULL;                 //You can use ++ in this loop,
       }                          //StrDest ='0'; Is it necessary?
return address;               //To implement the chain operation, the destination address is returned
}

Strcpy function prototype:

char *strcpy(char *strDest, const char *strScr)
{
       char *address=strDest;
       assert((strDest != NULL) && (strScr != NULL));
       while(*strScr)                   //Is the while (* strScr! = '0');
       {
              *strDest++ = *strScr++;
       }
       *strDest = '0';                       //When the strScr string length is less than the original strDest string length
       return address;                      //If you do not change the statement, you will make an error.
}

STRCMP function prototype:

int strcmp (const char *str1,const char *str2)
{           
       int len = 0;
       assert((str1 != '0') && (str2 != '0'));
       while(*str1 && *str2 && (*str1 == *str2))
       {
              str1++;
              str2++;
       }
       return *str1-*str2;
}

Strlen function prototype:

int strlen(const char *str)
{
    int len = 0;
       assert(str != NULL);
       while(*str++)
       {
              len++;
       }
       return len;
}


Related articles: