The C programming language Reading Notes 2

Source: Internet
Author: User

1. pointer

1.1 Use of auto-incrementing characters

+ + * P; // Add one to the content pointed to by p

(* P) ++; // Add one to the content pointed to by p

* P ++; // p self-incrementing

* ++ P; // p itself auto-incrementing

This is because the unary operators such as * And ++ combine the operation components in the order from right to left when the expression is evaluated.

1.2 pointer operations are faster than array subscript operations

1.3 array name

An array name is the location of the array's 0th elements. Therefore, the value assignment statement pa = & a [0] is equivalent to pa =.

1.4 evaluate the array subscript

When calculating the value of array element a [I], the C language actually converts it to the form of * (a + I) and then calculates the value.

For pointer pa, pa [I] is equivalent to * (pa + I)

1.5 differences between the array name and the pointer pointing to the first address of the array

The former is not a variable, and the latter is a variable.

1.6 function parameter input pointer

In the function definition, char s [] is equivalent to char * s.

When a function parameter is passed into a pointer to a sub-array of a large array, it does not affect the function itself.

1.7 use negative subscript for pointer

If you are sure that an element exists, expressions such as p [-1] and p [-2] are syntactically valid.

 b[] = {,,,,,,,,, *a = b + ;

Then: a [-3] = 1; a [-2] = 2;

1.8 stack Management of alloc and afree

First allocated and then released

Before calling alloc:

Allocbuf:

After calling alloc:

Allocbuf:

Afree (p) points the starting pointer of the idle unit to the position p.

1.9 comparison

You can perform comparative operations on Pointer values when they are meaningful.

   b[] = {,,,,,,,,, *p1 = &b[ *p2 = &b[];

P2-p1 = 4

1.10 Effective pointer-related operations

1.11 strcpy Function

Functions in the original book:

 strcpy(  *s,  *( *s++ = *t++

The pointer variable of s is incorrect. Although the content is copied, the first address of the content in this segment cannot be found. It should be:

 *strcpy(  *s,  *     *( *cp++ == *t++

1.12 function parameters are two-dimensional arrays

F (int daytab [2] [13])

= F (int daytab [] [13])

= F (int (* daytab) [13])

Input a pointer. The content of each element of the pointer is an array.

1.13 pointer to array and pointer Array

  Int (* daytab) [13] --> the pointer points to an array of int [13 ].

Int * daytab [13] à 13 pointer to int

The essence of pointer is to parse the data in memory according to the defined type and the address stored in it

Print (Int * s ){

S [1] = 5; // it is * (s + 1) = 5;

};

* (S + 1) = 5;

Change the data in the pointer Based on the address and the defined type.

Int (* daytab) [13]

Array of the defined type int [13]

Therefore, the first address of the 13 int-long memory areas is obtained by referencing the pointer.

That is:

Int a [4] [4]; int (* B) [4] =;

  

B [2] [2] = * (B + 2) + 2) = (* (s + 2) [2]! = * (S + 2) [2]

Analysis:

* B obtains the following result after unreferencing: int [4].

* The value of B is the first address of this int [4 ].

** B indicates the content pointed to by the first address of this int [4], that is, the value of B [0] [0 ].

1.14 initialize the pointer Array

  Uses pointers to initialize arrays.

1.15 pointer to function

1.15.1 one instance:

Supports the-n sorting function for numbers.

 #include <stdio.h> #include <.h>  MAXLINES 5000 /* max #lines to be sorted */  *lineptr[MAXLINES];   readlines( *lineptr[],   writelines( *lineptr[],   qsort( *lineptr[],  left,   (*comp)( *,  *  numcmp( *,  *  main( argc,  *       nlines;       numeric = ;       (argc >  && strcmp(argv[], )==          numeric =       ((nlines = readlines(lineptr, MAXLINES)) >=          qsort((**) lineptr,          (int (*)(void*,void*))(numeric ? numcmp : strcmp));                }          printf(            }

  

   qsort( *v[],  left,  )            swap( *v[], ,       (left >= right)          ;      swap(v, left, (left + right)/     last =      (i = left+; i <= right; i++          ((*comp)(v[i], v[left]) <              swap(v, ++       qsort(v, last+ }

 

1.15.2 pointer to the Function & the function returned as the pointer

Pointer to the function:

Int (* comp) (void *, void *);

Returns a pointer to a function:

Int * comp (void *, void *);

C. Each function has an entry address during compilation. The entry address is the address pointed to by the function pointer.

With the pointer variable pointing to the function, the pointer variable can be used to call the function.

1.15.3 function pointer Definition

1. void (* f) (int x );

Call (* f) (x );

2. typedef int (* fun_ptr) (int, int );

Fun_ptr max_func = max;

C = max_func (a, B );

1.15.4 function pointer Array

  1. Standard definition:

Int (* op [2]) (int, int );

2. Forced type conversion

It is defined as a normal int-type pointer array.

Force type conversion to corresponding function pointer type during use

Int *;

Int add (int a, int B );

A = add;

R = ((Int (*) (int, int)) (A) (numa, numb );

1.16 understanding of complex pointers

Basic Form:

Char (* x () []) ()

* X () indicates the function whose return value is a pointer.

The rest is the type of the returned pointer:

The returned pointer type is 6 in the above formula. The returned char function pointer Array

So,

X is a function. The return value of this function is a pointer to a function pointer array of char type.

Char (* x [3]) () [5]

(* X [3]) () the above formula 6 is a function pointer array.

Other parts describe the return value of the function pointer array.

Therefore, x is a function pointer array whose return value is char [5] and its length is 3.

 

2. Memory Layout

2.1 memory layout of Arrays

  Int a [10];

  

Memory is the adjacent area for continuous allocation.

Int * p = &;

P = p + 1;

2.2 big-end mode & Small-end Mode

Big-end mode: refers to the high level of data, which is stored in the low address of the memory, while the low level of data is stored in the high address of the memory.

Example:

65534 (0x0000FFFE)

Small-end mode (one-byte storage unit ):

0x0000 0x0008 0x0010 0x0018

Fe ff 00 00

Big end mode:

0x0000 0x0008 0x0010 0x0018

00 00 FF FE

 

3. Structure

3.1Definition of Structure Variables

Struct {...} X, y, z;

Syntactically similar to int x, y, z;

3.2 Definition of Structure Variables

Typedef struct {...} MyType;

3.3 initialization of Structure Variables

Struct point maxpt ={ 320,200 };

3.4 legal operations on struct  

3.5 structure array Initialization

 *= {“auto”,,””,};

3.6-byte alignment

  Participate in the blog article about the byte alignment problems caused by struct members in the C Language

 

4. Miscellaneous

4.1Comma expression

The comma operator has the lowest priority level. It connects two formulas. In the process of solving the comma operator, expression 1 is used, expression 2 is used, and the whole expression is the value of expression 2.

(3 + 5, 6 + 8) = 14

(A = 3*5, a * 4) = 60

4.2 command line parameters

Number of parameters in the command line during argc Program Execution

Argv pointer to String Array

4.3 Union

Manage different types of data in a separate storage area

4.4Bit Field

 is_keyword :  is_extern  :  is_static  : 

The size of the allocated space is the maximum multiple of the Field Types allocated by flags, which is then stored in one byte. The storage method in bytes is unknown. Depending on the compiler, you can choose from right to left or from left to right. GCC is from right to left.

 

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.