Part IV • Documenting the Differences
malloc()
Header: |
malloc.h & stdlib.h |
Syntax: |
void * malloc(size_t SizeToAllocate); |
Description: |
Allocates memory (uninitialized). |
Parameters: |
SizeToAllocate—Size of the memory block to allocated. |
Returns: |
Pointer to the memory block allocated or NULL if the memory |
|
could not be allocated.. |
Example: |
int * nArray; |
nArray = (int *)malloc (sizeof(int) * 500);
memset(nArray, 0, sizeof(int) * 500)
Note: Memory allocated using malloc() is never initialized; however, memory allocated using calloc() is.
mblen()
Header: |
stdlib.h |
Syntax: |
int mblen(const char * szString, size_t |
|
nCount); |
Description: |
Examines nCount characters, starting from the location pointed to |
|
by szString, looking for the number of bytes in the next multibyte |
|
character. |
Parameters: |
szString—Pointer to the first byte of the multibyte character. |
Returns: |
Zero if szString is NULL, -1 if this is not a multibyte character or |
|
the number of characters in the multibyte character. |
Example: |
int nCount; |
|
nCount = mblen(szString, 3); |
Note: |
See the other multibyte character functions, which follow. |
ANSI C’s Library Functions |
C C C |
|
14C |
|
C C C |
|
C C |
mbstowcs()
Header: stdlib.h
Syntax: size_t mbstowcs(wchar_t * pWideChar, const
char * szString, size_t
nCount);
Description: Converts the multibyte characters pointed to by szSting into wide character codes and places the result into pWideChar, converting up to nCount characters.
Parameters: pWideChar—Pointer to a string buffer to receive the wide character conversion.
szString—Source multibyte character string.
nCount—Size of pWideChar.
Returns: Number of characters converted or -1 if an error occurs.
Example: No example provided.
Note: Also see mblen() and mbtowc().
mbtowc()
Header: stdlib.h
Syntax: int mbtowc(wchar_t * pWideChar, const char * pMultiByte, size_t nCount);
Description: Converts a single multibyte character pointed to by pMultiByte into a wide character code and places the result into pWideChar, examining up to nCount characters.
Parameters: pWideChar—Pointer to a buffer to receive the wide character conversion.
pMultiByte—Source multibyte character string.
nCount—Size of pWideChar.
Part IV • Documenting the Differences
Returns: |
Number of characters converted or -1 if an error occurs. |
Example: |
No example provided. |
Note: |
Also see mblen() and mbtowcs(). |
memchr()
Header: |
memory.h & string.h |
Syntax: |
void * memchr(const void * szString, int |
|
chChar, size_t nLength); |
Description: |
Searches for the first occurrence of chChar in szString limiting |
|
the search to the first nLength characters. |
Parameters: |
szString—Pointer to the string to search. |
|
chChar—Character to search for. |
|
nLength—Number of characters to search in szString. |
Returns: |
Pointer to the located character or NULL if it cannot be found. |
Example: |
char szString[] = {“Now is the time for |
|
all good men”}; |
|
printf(“Is it the time %s”, |
|
memchr(szString, ‘f’, |
|
strlen(szString)); |
|
/* Will print Is it the time for all good men */ |
Note: |
See memcmp() and memset(). Notice that memchr() doesn’t assume |
|
the string is a NULL terminated character string. |
memcmp()
Header: |
memory.h & string.h |
Syntax: |
int memcmp(const void * pBuffer1, const void |
|
* pBuffer2, size_t nLength); |
Description: |
Compares up to nLength characters of pBuffer1 with pBuffer2, |
Part IV • Documenting the Differences
Returns: |
The pointer pDestination. |
Example: |
char |
szString1[] = {“Now is all the time |
|
|
for all good men”}; |
|
char |
szString2[] = {“Now is not the time |
|
|
for all good men”}; |
|
|
memcpy(szString1, szString2, |
|
|
strlen(szString2)); |
|
|
printf(“‘%s’ and ‘%s’”, |
|
|
szString1, |
|
|
szString2); |
Note: |
See memmove() and memset(). Notice that memcpy() should not be |
|
used where the source and destination overlap. |
memmove()
Header: |
string.h |
|
Syntax: |
void * memmove(void * pDestination, const |
|
|
void * pSource, size_t |
|
|
nLength); |
Description: |
Copies nLength bytes from pSource to pDestination. Source and |
|
destination may overlap. |
Parameters: |
pDestination—Pointer to the destination buffer. |
|
pSource—Pointer to the source buffer. |
|
nLength—Number of bytes to copy. |
Returns: |
The pointer pDestination. |
Example: |
char |
szString1[100] = {“Now is all the |
|
|
time for all good |
|
|
men”}; |
|
char |
szString2[100] = {“Now is not the |
|
|
time for all good |
|
|
men”}; |
Part IV • Documenting the Differences
Description: |
Converts the tm time structure to calendar time (Coordinated |
|
Universal Time). If the values are out of range, they are adjusted |
|
as necessary. |
Parameters: |
Time—Pointer to a tm time structure. |
Returns: |
A time_t structure. |
Example: |
struct tm Time; |
|
memset(Time, 0, sizeof(Time); |
|
/* fill in Time with values */ |
|
mktime(&Time); |
Note: |
Also see time(). |
modf()
Header: |
math.h |
|
Syntax: |
double modf(double dValue, double * |
|
|
dIntegral); |
Description: |
Computes the fractional and integral parts of dValue. |
Parameters: |
dValue—Real number for which integral and fractional parts are |
|
desired. |
|
|
dIntegral—Pointer to a double that will receive the integral part |
|
of dValue. |
Returns: |
The fractional part of dValue. |
Example: |
double |
dIntegral; |
|
double |
dFractional = modf(4.1234, |
|
|
&dIntegral); |
|
/* dIntegral will be 4, dFractional will be 0.1234 */ |
Note: |
See fmod(). |
offsetof()
Header: stddef.h
Part IV • Documenting the Differences
pow()
Header: |
math.h |
Syntax: |
double pow(double x, double y); |
Description: |
Raises x to the power y. |
Parameters: |
x—Number to raise to power y. |
|
y—Power to raise x to. |
Returns: |
x to the power y. |
Example: |
printf(“3 to the power 5 = %f\n”, |
|
pow(3.0, 5.0)); |
|
/* Prints: 3 to the power 5 = 243.000000 */ |
Note: |
See exp(). |
printf()
Header: |
stdio.h |
Syntax: |
int printf(const char * szFormat, ...); |
Description: |
Prints, to stdout formatted output as defined by szFormat. |
Parameters: |
szFormat—A format descriptor string. |
Returns: |
Number of characters written. If negative, then an error occurred. |
Example: |
printf(“The number one is %d\n”, 1); |
Note: |
See the section on printf() format codes at the end of this |
|
chapter. |
putc()
Header: |
stdio.h |
Syntax: |
int putc(int nChar, FILE * OpenFile); |
Description: |
Writes nChar to the stream file OpenFile. |