C programming notes day06 dynamic memory allocation, function pointers, variable length parameters
Source: Internet
Author: User
Dynamic Memory Allocation: header file stdlib. h
Malloc: Memory Allocation
Calloc: allocates memory and clears it.
Realloc: adjust the size of allocated memory blocks
Example:
Int * P = (int *) malloc (3 * sizeof (INT); // allocates memory. The first address is returned successfully, and the first address is returned if no value is returned.
Free (p); P = NULL; // release the allocated memory and modify the pointer value to avoid the occurrence of a wild pointer (pointing to an address, but the address has been recycled)
Const and pointer:
Const int * P; // the pointer is variable, and the data corresponding to the pointer cannot be modified.
Int * const P; // the pointer is unchangeable and the data corresponding to the pointer can be modified.
Const int * const P; // the pointer is unchangeable and the data corresponding to the pointer cannot be modified.
Pointer to the function:
Int add (int A, int B) {return a + B ;}
Int MUL (int A, int B) {return a * B ;}
INT (* A) (INT, INT) = NULL; // defines the function type pointer.
Int sum;
A = add;
Sum = A (3, 4); // 7
A = Mul;
Sum = A (3, 4); // 12
Typedef int (* pA) (INT, INT) pa1, pa2; // defines the function type pointer.
Pa1 = add;
Pa2 = Mul;
Sum = pa1 (3, 4); // 7
Sum = pa2 (3, 4); // 12
Variable Length Parameter usage: header file stdarg. h
Int max (INT count,...) // count is the number of subsequent parameters, not sure how many
{
Va_list V;
Va_start (v, count );
For (INT I = 0; I <count; I ++)
{
Int argi = va_arg (v, INT); // obtain the values of each parameter.
}
Va_end (v );
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service