Original link: http://www.orlion.ga/977/
One, numeric string conversion function
#include <stdlib.h>int atoi (const char *nptr);d ouble atof (const char *nptr); return value: conversion result
Atoi converts a part of a string that can be recognized as a decimal integer into an int, such as atoi (" -123abc"), which returns 123 (a string can have spaces at the beginning). If there is no recognizable integer at the beginning of the string returns 0, and Atoi ("0ABC") returns 0. (Atoi ("abc123") returns 0) Atof converts the part of a string that can be recognized as a floating-point number to a double type, such as: Atof ("31.4") returns 31.4. Neither Atoi nor atof can check for the wrong situation.
#include <stdlib.h>long int strtol (const char *nptr, char **endptr, int base);d ouble strtod (const char *nptr, char * *ENDPTR); return value: Convert result, set errno On Error
Strtol is an enhanced version of Atoi, mainly embodied in these aspects:
(1) Not only can recognize the decimal integer, but also can identify other binary integers, depending on the base parameter, such as Strtol ("0xdeadbee~~", NULL, 16) return the value of 0xdeadbee, Strtol ("0777~~", NULL, 8) Returns a value of 0777.
(2) endptr is an outgoing parameter that points to the first character that is not recognized after the function returns. For example Char *pos;strtol ("123abc", &pos,), Strtol returns 123,pos points to the letter A in the string. If there is no recognizable integer at the beginning of the string, such as Char *pos; Strtol ("Abcabc", &pos, 10);, then Strtol returns 0,pos points to the beginning of the string, which can be used to judge the situation of this error, which is atoi cannot handle.
(3) If the integer value in the string exceeds the representation range (overflow or underflow) of long int, then Strtol returns the largest (or smallest) integer it can represent and sets errno to Erange, for example strtol ("0xdeadbeef~~", null,16) Returns 0X7FFFFFFF and sets errno to Erange.
Second, memory allocation function
In addition to using malloc (), there are other functions that can be used to allocate memory outside the heap:
#include <stdlib.h>void *calloc (size_t nmemb, size_t size), void *realloc (void *ptr, size_t size); Return value: Return the first address of the allocated memory space successfully, error returns null
The parameters of the calloc are much like the fread/fwrite parameters, allocating the memory space of the NMEMB elements, each of which occupies a size byte, and calloc is responsible for populating the memory space with byte 0, and malloc is not responsible for zeroing out the allocated memory space.
Sometimes the memory space allocated with malloc or calloc needs to change its size after a period of time, one way is to call malloc to allocate a new memory space, copy the data from the original memory space to the new memory space, Then call free to release the original memory space. Using the REALLOC function simplifies these steps by passing the pointer ptr of the original memory space to ReAlloc, specifying the new size (in bytes) with the parameter size, REALLOC returns the first address of the new memory space, and frees up the original memory space. The data in the new memory space as far as possible consistent with the original, if size is smaller than the original, then the size of the previous byte is not changed, the subsequent data is truncated, if the size is larger than the original data are all preserved, the back of a chunk of memory space is not initialized (ReAlloc is not responsible for clearing 0). Note that the parameter PTR is either null or must be a pointer returned by a previous call to malloc, calloc, or realloc, and you cannot pass any pointers to realloc to require that the memory space be redistributed. As a two exception, if calling ReAlloc (NULL, size) is equivalent to calling malloc (size), if realloc (PTR, 0) is called, PTR is not NULL, which is equivalent to calling free (PTR).
#include <alloca.h>void *alloca (size_t size); return value: Returns the first address of the allocated memory space, if size is too large to cause stack space to run out, the result is undefined
The parameter size is the number of bytes requested, and instead of allocating space on the heap, the ALLOCA function allocates space on the stack frame of the caller's function, similar to the variable-length array of C99, which automatically releases the stack frame when the caller function returns, so no free is required. This function is not part of the C standard library, but is defined in the POSIX standard.
C Standard library-numeric string conversion and memory allocation functions