C language improvement review 01

Posted by burningkamikaze on Fri, 03 Sep 2021 19:46:19 +0200

Take time to review carefully:

Foundation: c language syntax foundation + data structure + system programming

Improvement: interface encapsulation and design (module division and fault thinking) + interface api use ability, search ability and implementation ability

Jump: win+linux project

Simple sorting function:

#include "stdlib.h"
#include "string.h"
#include "stdio.h"

//sort
void main01()
{
	int		i = 0,j = 0;
	int		tmp = 0;
	int a[] = {33,654,4,455,6,33,4};

	printf("Before sorting\n");
	for (i=0; i<7; i++)
	{
		printf("%d ", a[i]);
	}
	
	//sort

	//Outer circulation 	 When i=0, let j change from 1 = n
	//Outer circulation 	 When i=1, let j change from 2 = n
	//			When i=2, let j change from 3 = n
	//Conclusion: according to the invariance of one variable i, let the other variable j change; The next round is carried out in turn

	for(i=0; i<7; i++)  
	{
		for (j=i+1; j<7; j++)  //Inner circulation: a[i] and a[j] comparison
		{
			if (a[i] > a[j])
			{
				tmp = a[i];
				a[i]= a[j];
				a[j] = tmp;
			}
		}
	}

	printf("After sorting\n");

	for (i=0; i<7; i++)
	{
		printf("%d ", a[i]);
	}

	printf("hello...\n");
	system("pause");
}


//void printArray(int a[7], int num)
//void printArray(int a[], int num)
void printArray(int *a, int num)
{
	int i = 0;
	for (i=0; i<num; i++)
	{
		printf("%d ", a[i]);
	}
}

void sortArray(int a[7], int num)
//void sortArray(int a[], int num)
//void sortArray(int *a, int num)
{
	int i , j , tmp ;
	int		num2 = 0;

	num2 = sizeof(a)/sizeof(a[0]);
	printf("num:%d \n", num2);
	//The data types of a of the argument and a of the formal parameter are essentially different
	//The compiler will treat the array in the formal parameter as a pointer, which is a feature of C language
	for(i=0; i<num; i++) 
	{
		for (j=i+1; j<num; j++)  //Inner circulation: a[i] and a[j] comparison
		{
			if (a[i] > a[j])
			{
				tmp = a[i];
				a[i]= a[j];
				a[j] = tmp;
			}
		}
	}
}

//The return of array as function parameters is returned as a pointer, 
//Correct method: pass the memory first address of the array and the effective length of the array to the called function
//2 / / the data types of a of the argument and a of the formal parameter are essentially different
	//The compiler will treat the array in the formal parameter as a pointer, which is a feature of C language
	//The essence of sorting is also analyzed 
//Formal parameters written in a function are the same as those written in a function, except that they have external attributes
void main22()
{
	int		i = 0,j = 0;
	int		tmp = 0;
	int		num = 0;
	int a[] = {33,654,4,455,6,33,4,3333};
	num  = 7;

	num = sizeof(a)/sizeof(a[0]);
	printf("num:%d \n", num);

	printf("Before sorting\n");
	printArray(a, num);

	//sort

	//Outer circulation 	 When i=0, let j change from 1 = n
	//Outer circulation 	 When i=1, let j change from 2 = n
	//			When i=2, let j change from 3 = n
	//Conclusion: according to the invariance of one variable i, let the other variable j change; The next round is carried out in turn

	sortArray(a, num);

	printf("After sorting\n");
	printArray(a, num);

	printf("hello...\n");
	system("pause");
}

Output:

Before sorting
33 654 4 455 6 33 4 After sorting
4 4 6 33 33 455 654 hello...

Conclusion: 1. Array as a function parameter will degenerate into a pointer, a of the argument is an array, a passed to the formal parameter is a pointer, and the essence of the data type is different; The compiler will treat the array in the formal parameter as a pointer, which is a feature of C language,

2. And the formal parameters, like the variables in the function, are stored on the temporary stack. Formal parameters written in a function are the same as those written in a function, except that they have external attributes

Data type essence: alias of fixed memory size;

Function of data type: the amount of memory space allocated by the compiler budget object (variable)

  • Find the size of the data type sizeof(int *)

int b[10];

b represents the address of the first element of the array
&B represents the address of the entire array  

Data type alias:

//Complex data type
typedef struct Teacher2
{
	char name[64];
	int age;
}Teacher2;
//Data alias typedef

//Simple data type
typedef int u32;

Data type encapsulation:

                 int InitHardEnv(void **handle);

  • 1. Void literally means "no type", void * is "no type pointer", and void * can point to any type of data.
  • 2. Usage 1: encapsulation of data types
  •      Typical function prototypes such as memory operation functions memcpy and memset are

                      void * memcpy(void *dest, const void *src, size_t len);

                      void * memset ( void * buffer, int c, size_t num ); 

  • 3. Usage 2:   void modifies the return value and parameters of a function, only indicating none.
  •        If the function does not return a value, it should be declared as void

           If the function has no parameters, its parameters should be declared void

           int function(void)

           {return 1;}

  • 4. Meaning of void pointer  
  •       C language stipulates that only pointers of the same type can be assigned to each other

          The void * pointer is used as an lvalue to "receive" any type of pointer

          A void * pointer needs to be cast when it is assigned to other pointers as an R-value

          int *p1 = NULL;

          char *p2 = (char *)malloc(sizoeof(char)*20);

  • 5. There is no variable of type void
  •       The C language does not define how much memory void is an alias

  • 6. Extended reading of detailed explanation of void types.doc

Data type summary and extension

  • 1. Data type is essentially an alias of fixed memory size; It is a mold. c language stipulates that variables are defined by data types.
  • 2. Data type size calculation (sizeof)
  • 3. You can alias an existing data type typedef
  • 4. Data type encapsulation concept (void universal type)

Variable nature:

Variable: a memory object that can read and write, called a variable; Objects that cannot be modified once initialized are called constants.

Variable nature:

1. The program applies for and names the memory space int a = 0 through variables

2. Access memory space by variable name

Alias of (a contiguous) memory space (which is a house number)

3. How many ways to modify variables?

1. Directly

2. Indirect. There is an address number in the memory. You can also modify the memory after getting the address number; So he was born( Programming case)

3. Can the memory space be given another alias? quote

//Important experiments:
int  main333()
{
	//
	//There are two methods to directly operate memory through variables
	//	Operate memory by memory number

	int i = 0;

	printf("&i:%d\n", &i);

	*((int *)(1245024)) = 10;
	printf("i:%d", i);
	printf("hello....\n");
	getchar();
	return 0;
}

Variable summary:

1 pair of memory, readable and writable; 2 read and write data to memory through variables; 3 instead of reading and writing data to the variable, write data to the memory space represented by the variable. Q: where are the variables?

Thinking 1: three elements of variables (name, size and scope), and the life cycle of variables?

Think 2: how does the C + + compiler manage the relationship between function 1 and function 2 variables?

Establishment process of four memory areas:

Process description

1. The operating system load s the physical hard disk code into memory

2. The operating system divides the c code into four areas

3. The operating system finds the main function entry to execute

  String constants are in the global area.

The key to understanding pointers is memory

The growth direction of stack is different from that of b in array b[10].


 

Topics: C