C based learning notes

Basic data types in C

  • char 1 byte (not represented by kanji)
  • short 2 bytes
  • int 4 bytes
  • long 4 bytes (long numbers can longlongs)
  • float 4 bytes
  • double 8 bytes
  • signed (it can only be used to modify the integer)
    • The default is signed int
  • unsigned (it can only be used to modify the integer)
  • void (not directly defined variables, but may define a pointer)
    • void * can point to any object

note:

  1. C language and is not boolean type byte (0 represents false, 0 represents a non-true)
  2. [String] stated: char * str = "Hello, C"

Pointer operations

  • A pointer variable declaration: int * pointer; double * d_pointer;
  • Two pointer variable declarations: int ** pointer; (can only be used to store the address of a pointer variable, and so multi-level indicators)
  • Pointer value takes a variable (&): int * pointer = & i;
  • Modifying the pointer points to the memory variable values: * pointer = 123;
  • A plurality of function return values ​​may be implemented (reference is to use transfer) by a pointer
  • String stated: char * str = "Hello, C" (note that when printf, taking the value of the variable can not add * str, directly str)
  • Pointer variable plus 1 plus 2 doing this operation, move the pointer value is n bytes (depending on the number of bytes occupied by the type of pointer variable)
  • If the parameter is a function pointer variable, and the function to modify the address value of the pointer variable, then the parameters of the function to be declared as two pointers, otherwise the changes will not succeed

note:

  1. Wild pointers are not directly assigned
  2. Pointer type stated value corresponding to a memory address to be consistent, otherwise an error value, such as:
    Double I = 3.14; & I = int * pointer; this value then will pass an error * pointer, double * point must be declared
  3. Share a pointer variable memory size: 32-bit program accounted 4byte, 64-bit program accounted 8byte

RAM

Stack memory

  • Created on the stack. During a function, the function of the storage unit can create a local variable on the stack, the memory cells are automatically released at the end of the function execution. Stack memory allocation operation in the processor instruction set, high efficiency, but the limited amount of memory allocated.
  • return statement function, be careful not to return to point to "stack memory" and "pointer" or "reference", because the memory is automatically destroyed at the end of the function body.

Heap

EVERYTHING

Static memory

EVERYTHING

Structure

1. How to define

Keywords: struct (similar java in the class)

struct Student {
    short age;
    int score;
};

2. How to declare and use

struct Student {
    short age;
    int score;
};

main() {
    struct Student stu = {18, 100};
    printf("分数:%d", stu.score);
}

3. The structure occupies memory

  • Structure size> = structure occupied by each of a variable number of bytes and
  • Structure occupying the structure size = the maximum integer multiple of that variable (for memory alignment to facilitate bit operation)

4. C structure can not be defined function in

  • C structure can not be defined in function, but may be defined through a function pointer
    • Define rules: Return value (* function pointer variable name) (parameter)
void study() {
 printf("day day up!");
}

struct Student {
    short age;
    int score;
    
    void(*study_pointer)();
};

main() {
    struct Student stu;
    stu.study_pointer = &study;
    stu.study_pointer();
}

The indirect reference operator

  • Symbols: ->
  • Action: Use this operator to make direct calls to the structure pointer variable elements in structures
struct Student {
    short age;
    int score;
}

main() {
    struct Student stu = { 18, 100 };
    struct Student* stu_pointer = &stu;
    printf("分数:%d", stu_pointer -> score);
}

Commonwealth

  • Keywords: union
  • Action: If a value of a data type may sometimes, sometimes another data type, then the time can be used to Commonwealth, to save memory effect
  • Memory footprint: Consortium for the largest share of memory types, such as cases occupies four bytes (the number of bytes to take maximum occupancy of type int)
union result {
    int code;
    char flag;
}

main() {
    union result r;
    r.code = 1;
    printf("%d", r.code);
}

enumerate

enum MyEnum {
	ONE, TWO
};

main() {
	enum MyEnum e = ONE;
	printf("%d", e); // 0
}

Custom Types

  • Keywords: typedef
  • Role: to take individual variable name
typedef struct Student {
    short age;
    int score;
} stud;

main() {
    stud stu = { 18, 100 };
    printf("分数:%d", stu.score);
}
Published 12 original articles · won praise 3 · Views 2442

Guess you like

Origin blog.csdn.net/AWen_Jack/article/details/104613310