Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

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.

556

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.

557

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,

558

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

Parameters:

pBuffer1—Pointer to the first buffer.

C C

 

 

pBuffer2—Pointer to the second buffer.

 

 

nLength—Number of bytes to compare.

 

Returns:

Zero if they are equal, < 0 if pBuffer1 is less than pBuffer2,

 

or > 0 if pBuffer1 is greater than pBuffer2.

 

Example:

char

szString1[] = {“Now is all the time

 

 

 

for all good men”};

 

 

char

szString2[] = {“Now is not the time

 

 

 

for all good men”};

 

 

 

if (memcmp(szString1, szString2,

 

 

 

strlen(szString1)) == 0)

 

 

 

{

 

 

 

printf(“‘%s’ ‘%s’ are equal”,

 

 

 

szString1,

 

 

 

szString2);

 

 

 

}

 

 

/* Will not print since the strings are not

 

 

equal */

 

Note:

See memchr() and memset(). Notice that memcmp() doesn’t assume

 

the string is a NULL terminated character string.

 

memcpy()

 

 

 

Header:

memory.h & string.h

 

Syntax:

void * memcpy(void * pDestination, const

 

 

 

void * pSource, size_t

 

 

 

nLength);

 

Description:

Copies nLength bytes from pSource to pDestination. Source and

 

destination must not overlap.

 

Parameters:

pDestination—Pointer to the destination buffer.

 

 

pSource—Pointer to the source buffer.

 

 

nLength—Number of bytes to copy.

 

559

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”};

560

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

 

 

memmove(&szString2[10], szString2,

C C

 

 

 

 

 

strlen(szString2));

 

 

 

printf(“‘%s’”,

 

 

 

szString2);

 

Note:

See memcpy() and memset(). Notice that memcpy() should not be

 

used where the source and destination overlap, while memmove()

 

works correctly when there is overlap; however, this function may

 

be slower.

 

memset()

 

 

 

Header:

memory.h & string.h

 

Syntax:

void * memset(void * pBuffer, int nByte,

 

 

 

size_t nLength);

 

Description:

Fills nLength bytes of pBuffer with nByte.

 

Parameters:

pBuffer—Buffer that is to be filled.

 

 

nByte—Byte to fill the buffer with.

 

 

nLength—How many bytes to fill.

 

Returns:

The pointer pBuffer.

 

Example:

int

* Array;

 

 

 

Array = (int *)malloc

 

 

 

(sizeof(int) * 100);

 

 

 

memset(Array, 0, sizeof(int) * 100);

 

 

/* Zeros out the allocated array */

 

Note:

This function is very useful to initialize both auto and allocated

 

data objects, which are not otherwise initialized.

 

mktime()

 

 

 

Header:

time.h

 

Syntax:

time_t mktime(struct tm * Time);

 

561

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

562

 

ANSI C’s Library Functions

 

C C C

 

 

 

14C

 

 

 

C C C

Syntax:

size_t offsetof(composite Structure, name

 

C C

 

 

 

Member);

 

 

Description:

Returns the offset (in bytes) of Member from the beginning of

 

Structure.

 

 

Parameters:

Structure—A structure.

 

 

 

Member—A member in Structure.

 

 

Returns:

Offset in bytes.

 

 

Example:

struct tm Time;

 

 

 

printf(“the offset of tm_year is %d”,

 

 

 

offsetof(struct tm, tm_year));

 

 

Note:

Remember that Structure is the type name, not the variable

 

name.

 

 

perror()

 

 

 

Header:

stdio.h & stdlib.h

 

 

Syntax:

void perror(const char * szPrefix);

 

 

Description:

Prints an error message corresponding to errno to stderr. If

 

szPrefix is not NULL, that string is prefixed to the message

 

printed.

 

 

Parameters:

 

 

 

Returns:

Zero if successful, otherwise a non-zero value.

 

 

Example:

/* Set errno to an error value (usually set by a library

 

function) */

 

 

 

errno = EACCES;

 

 

 

perror(“DUMMY ERROR HERE”);

 

 

 

/* prints: DUMMY ERROR HERE: Permission denied

*/

 

563

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.

564

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

Parameters:

nChar—Character to be written.

C C

 

 

OpenFile—Pointer to an opened file.

 

Returns:

Character nChar if successful, otherwise EOF.

 

Example:

putc(‘A’, stdout);

 

Note:

Same as fputc() except that putc() can be implemented as a

 

macro.

 

putchar()

 

 

Header:

stdio.h

 

Syntax:

int putchar(int nChar);

 

Description:

Writes nChar to the stream file stdout.

 

Parameters:

nChar—Character to be written.

 

Returns:

Character nChar if successful, otherwise EOF.

 

Example:

putchar(‘A’);

 

Note:

Same as fputc(nChar, stdout) except that putc() can be imple-

 

mented as a macro.

 

puts()

 

 

Header:

stdio.h

 

Syntax:

int puts(const char * szString);

 

Description:

Writes szString to the stream file stdout.

 

Parameters:

szString—Pointer to the character string to be written.

 

Returns:

Non-zero positive value if successful, otherwise EOF.

 

Example:

putchar(“Now is the time.\n”);

 

Note:

Also see fputs() and putc().

 

565