Byte Operations in C

There is a need to manipulate user-defined strings, i.e., strings that are not the standard C character strings which are terminated by null bytes. Even though user-defined strings can have null bytes, but they rarely represent a need to terminate the string. Instead of using functions from string.h, we should use byte manipulation functions, bcopy(), bzero(), and bcmp() defined in the library strings.h. The usage is shown below:

type_t user_defined_str = ...;
type_t another_str = ...;

// copy sizeof(another_str) bytes from user_defined_str to another_str
bcopy(user_defined_str, another_str, sizeof(another_str));

// compare sizeof(another_str) bytes between user_defined_str and another_str
// if the result is 0, then they are equal, otherwise they are not.
if (bcmp(user_defined_str, another_str, sizeof(another_str) == 0) {
  ...
}

// write sizeof(type_t) null bytes into another_str
bzero(another_str, sizeof(type_t));

These Byte Operations could be useful in Socket Programming. However, the above functions are deprecated in favour of memcpy(), memmove(), memset(), and memcmp() defined in string.h for portability.

#memory