C语言 笔记

C语言 笔记


pointers are variables that store address of another variable

(1)Declaration and Assignment

Two methods:

This is NOT a referencing:

(2) Dereference

(3)Pointer Arithmetic


(4)Pointer to Pointer

(5)Pointer and Function

when we use this increment function:

Because a is local variable, we can only access a in the same function scope. So we change it to:

Memory Allocator

The stack has a limited size so we normally allocate memory on the heap

(1) malloc

Takes a single argument, the size of the memory to allocate (in bytes) and returns the address of our newly allocated memory as a void pointer. Note: C does no range checking

When memory is allocated using malloc(), there is no guarantee that the memory will be set to zeros, but calloc() do

(2) calloc

(3) realloc

If you’ve already allocated some memory but find you actually need more memory, there is a function that will either extend the size of your allocation, or allocate a new bigger block elsewhere in memory and copy the old data across

(4) free

The free() function returns no value and simply releases the memory in use by a pointer. After free has been called the memory at *ptr will be released. The value of ptr will not change, however, so many people choose to explicitly set ptr to null to prevent any invalid memory accesses.



String do not really exists in C, the string “hello” in C is an array of 7 characters: [“h”,”e”,”l”,”l”,”o”,0x00]