A brief introduction to the related functions of the memory address operation of string in C language _c language

Source: Internet
Author: User

C language Bcopy () function: Copy memory (String)
header file:

#include <string.h>

The bcopy () function is used to copy memory (a string), and its prototype is:

 void bcopy (const void *src, void *dest, int n);

"Parameter" SRC is the source memory block (string) pointer, dest is the target memory block (string) pointer, n is the first n bytes length of the memory (string) to be copied.

Bcopy () is the same as the memcpy () is used to copy SRC refers to the memory content of the first n bytes to the address of the dest, but the parameter src and dest in the opposite position when passed to the function.

Bcopy () does not check null bytes in memory (string).

In fact, the bcopy () and memcpy () functions are the same, used to copy the first n bytes of the memory block, but S1, S2 two parameters as pointers, and strangely in the String.h file, can also be used to copy strings.

Note: bcopy () is not a standard function, not defined in ANSI, the author failed to compile under VC6.0 and MinGW5; it is said that the GCC support under Linux, but the author did not close test. In view of this, use memcpy () instead.

For more information, see the differences between the C language bcopy () and memcpy (), bzero () and memset (), bcmp () and memcmp () functions

The author defines a macro that is compiled and passed under VC6.0, with the following code:

#include <stdio.h>
#include <string.h>
#define BCOPY (A, B, c) memcpy (A, B, c)
main () {
 Char DEST[30] = "string (a)";
 Char src[30] = "string\0string";
 int i;
 Bcopy (SRC, dest, 30); The src pointer is placed in front of
 printf ("Bcopy ():");
 
 for (i = 0; i < i++)
  printf ("%c", Dest[i]);
 
 memcpy (dest, SRC, 30); Dest pointer on money
 printf ("\nmemcpy ():");
 
 for (i = 0; i < i++)
  printf ("%c", Dest[i]);

Execution results:

Bcopy (): String (a)
memcpy (): string (a)

C language Bzero () function: Zero the first n bytes of memory (string)
header file:

#include <string.h>

Bzero () zeros out the first n bytes of a memory block (string), and its prototype is:

 void bzero (void *s, int n);

The parameter S is a memory (string) pointer, and n is the number of bytes that need to be zeroed.

Bzero () sets the first n bytes of the memory area that the parameter S refers to, all to a zero value.

In fact, bzero (void *s, int n) is equivalent to memset ((void*) s, 0,size_tn), which zeros out the first n bytes of the memory block, but the s parameter is a pointer and is strangely located in the String.h file, or it can be used to clear 0 strings.

Note: bzero () is not a standard function, not defined in ANSI, the author failed to compile under VC6.0 and MinGW5; it is said that the GCC support under Linux, but the author did not close test. In view of this, use memset () instead.

C language bcmp () function: Compares the first n bytes of memory (string) to be equal
header file:

#include <string.h>

BCMP () compares the first n bytes of the memory (string) to be equal, and its prototype is:

 int bcmp (const void *S1, const void * s2, int n);

"Parameters" S1, S2 to two memory (or two strings) to be compared, n is the length to compare.

return value if S1, the first n bytes of S2 are equal or n is equal to 0, returns 0, otherwise it returns a value other than 0.

The BCMP () function does not check for null.

In fact, the BCMP () and memcmp () functions are the same, used to compare the first n bytes of the memory block for equality, but S1, S2 two parameters as pointers, and strangely in the String.h file, can be used to compare strings.

Note: bcmp () is not a standard function, not defined in ANSI, the author failed to compile under VC6.0 and MinGW5; it is said that the GCC support under Linux, but the author did not close test. In view of this, use MEMCMP () instead.


Let's just give an example:
Copy a plain text new window

#include <stdio.h>
#include <string.h>
int main ()
{
 char *s1 = "Golden Global View";
 Char *s2 = "Golden Global View";
 if (!bcmp (S1, S2, 7))
  printf ("S1 equal to S2 in the 7 bytes");
 else
  printf ("S1 not equal to S2 in 7 bytes");
 return 0;
}

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.