Buffer Manipulation Functions in C
C offers some buffer manipulation functions which directly operates on blocks of memory. For e.g, memcpy is a function designed to copy a chunk of memory of specified length ( number of bytes ) from one location ( memory address ) to another. The functionality of memcpy is similar to strcpy but the latter stops copying the contents once a NULL character is encountered. However, memcpy doesn't consider the NULL character and simply copies the chunk of memory. Thus, buffer manipulation functions are much efficient when it comes to performance.Following table shows some of the commonly used buffer manipulation functions.
Following program illustrates the use of memset function :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Array after allocation :-
0 0 0 0 0
Array after initialization :-
0 0 0 0 0
0 0 0 0 0
Array after initialization :-
0 0 0 0 0
----------------------------------------------------------------------------------------------------------------------------
Following program illustrates the use of memcpy and memcmp function :
----------------------------------------------------------------------------------------------------------------------------
Output :
str1 : csegeek
str1 is copied to str2
str2 : csegeek
str1 and str2 are equal
str1 is copied to str2
str2 : csegeek
str1 and str2 are equal
----------------------------------------------------------------------------------------------------------------------------
For case insensitive comparison of strings, we can use memicmp function. There are other buffer manipulation functions which we haven't discussed here but you can always refer to them in header file documentation.
No comments:
Post a Comment