C Programming Language
Contents
The C Programming Language is the best textbook for a beginner who wants to learn C. This article mostly talks about pointers and memory in C.
C’s syntax is simple but handling pointers in C is too easy to get stuck.
Primitive data types and sizes
char, int, float, double
signed, unsigned, short, long
constant
|
|
Array
Array is a continuous fixed-length address. Different data types have different sizes. sizeof
returns the size(how many bytes it holds to represent a data) of a data.
|
|
Buffer overflow
If you attemptes to write data beyond the bounds of array or buffer.
|
|
String
A string in C is actually an array of char
type, teminated with the '\0'
charactor, which equals to 0. So you will see this code often.
|
|
What’s the difference between char name[10]
and char *name
?
Array of strings
|
|
Pointer
A pointer is a variable contains another variable’s address in memory.
Technically speaking, memory is a large lenght array of continuous bytes. A 32-bit machine can maximumly have 2^32=4294967296 bytes, which is 4GB.
A pointer is a variable, so it has a type. &
is used to get the address of a variable. *
is used to get the content in a pointer. void *
is a special pointer, which can point to any type.
NULL
pointer in C is defined as (void *) 0
.
|
|
Memory leak
Allocated memory dynamically but fails (or forget) to release that memory when it’s no longer needed.
Array and pointer
A name of an array is actually the address of that data.
|
|
name+1
is the address of the second element in array name
, so if a pointer p
points to the location of name
, p+1
then points to the next address in memory.
Dynamic array
When you can’t decide the lenght of an array at compile time, you can allocate dynamic memory at runtime.
|
|
Structs
|
|
Library and system call
Reference
Author Paisen
LastMod 2018-04-05