About string. h

Source: Internet
Author: User

When I read the code today, I am a little dizzy. Maybe I don't know enough about string. h. I'm sorry for my C. Check the remedy.

1. Differences between strdup and strcpy:

Strdup
Prototype: extern char * strdup (char * s );
Usage: # include <string. h>
Function: Copy string s
Note: The pointer to the copied string is returned. The space required is allocated by malloc () and can be released by free.
Example:
// Strdup. c

# Include <syslib. h>
# Include <string. h>
Main ()
{
Char * s = "golden Global View ";
Char * D;

Clrscr ();

D = strdup (s );
Printf ("% s", d );

Getchar ();
Return 0;
}
Strcpy
Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # include <string. h>
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Example:
// Strcpy. c
# Include <syslib. h>
# Include <string. h>

Main ()
{
Char * s = "golden Global View ";
Char d [20];

Clrscr ();

Strcpy (D, S );
Printf ("% s", d );

Getchar ();
Return 0;
}
Strdup can directly copy the content to be copied to a non-initialized pointer, because it will automatically allocate space to the destination pointer.
The target pointer of strcpy must be the allocated memory pointer.

 

Strdup is not a standard C function, so an error is reported in Linux!
Strcpy is a standard C function. In Windows, an error is reported because the pointer does not apply for space!
You can use strlen to determine the from size, and then apply for space for to. Then, strcpy will not report an error!

2. Differences between strcmpi, stricmp, and strcmp:

Prototype: extern int strcmp (const void * S1, const void * S2 );

Usage: # include <string. h>

Function: Compare the case sensitivity of strings S1 and S2.

NOTE: If S1 = S2, zero is returned; otherwise, a non-zero value is returned.

Prototype: extern int stricmp (char * S1, char * S2 );

Usage: # include <string. h>

Function: Compares S1 and S2 strings, but does not distinguish between uppercase and lowercase letters.

Note: strcmpi is the macro definition to stricmp. This function is not provided in reality.
When S1 <S2, return value <0
When S1 = S2, the return value is 0.
When S1> S2, return value> 0
*/

// Example:

Void main ()
{
Char * str1 = "I am oldwolf ";
Char * str2 = "I am oldwolf ";
Int CMP;

Printf ("original strings:/n % s/n", str1, str2 );
CMP = strcmp (str1, str2 );
If (CMP! = 0)
Printf ("strcmp comparison strings are different! /N ");
Else
Printf ("The strcmp comparison string is the same! /N ");
CMP = stricmp (str1, str2 );
If (CMP! = 0)
Printf ("stricmp comparison strings are different! /N ");
Else
Printf ("The stricmp comparison string is the same! /N ");
CMP = strcmpi (str1, str2 );
If (CMP! = 0)
Printf ("strcmpi comparison strings are different! /N ");
Else
Printf ("strcmpi compare strings are the same! /N ");
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.