当前位置:网站首页>Su embedded training - Day5

Su embedded training - Day5

2022-07-08 00:58:00 Light chasing rain

One 、 Pointers and two-dimensional arrays

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//&a: The name and address of the two-dimensional array represent the upgrade of the address , Change the row pointer to a pointer to the entire array 
	printf("&a = %p\n",&a);
	printf("&a = %p\n",&a + 1);

	printf("a = %p\n",a);
	printf("a = %p\n",a + 1);
	//*a: Two dimensional array name * Indicates that the address is degraded ,
	// Demote row pointer to column pointer , here *a The operating space of is 4 Bytes 
	printf("*a = %p\n",*a);
	printf("*a = %p\n",*a + 1);
	
	printf("**a = %d\n",**a);
	printf("*(*a+1) = %d\n",*(*a+1));
	//a[i][j]------> *(*(a + i) + j)
	
	int i,j;
	for(i = 0 ; i < 3;i++)
	{
		for(j = 0 ; j < 4;j++)
		{
			//printf("%-5d",a[i][j]);
			printf("%-5d",*(*(a+i) + j));
		}
		putchar(10);
	}
	return 0;
}

Two 、 Array pointer

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

	// First level pointer p Only 4 Bytes , But the array name operation space of a two-dimensional array is the size of a row of data , That is to say 16 Bytes , So I can't 
	// Use the first level pointer to save the first address of the two-dimensional array 
	//int *p = a;  // error 
	// The secondary pointer is used to save the address of the primary pointer , The two-dimensional array name is a row pointer , Inconsistent nature and operation space 
//	int **p = a; // error 
	//
	// The real way to save the address of the array name of a two-dimensional array or define a row pointer is to define an array pointer 
	// Array pointer : Essence is a pointer , The pointer points to a two-dimensional array , Is to define a row pointer 
	// Format :  data type  (* Variable name )[ Number of columns ]
	// purpose :  The real purpose of array pointer is to talk about a two-dimensional array passed to the specified function by passing parameters 
	int (*p)[4] = a;

	int i,j;
	for(i = 0 ; i < 3;i++)
	{
		for(j = 0 ; j < 4;j++)
		{
			printf("%-5d",p[i][j]);
		}
		putchar(10);
	}
	return 0;
}

3、 ... and 、 Pointer array

Pointer array : The essence is an array , Each element of the array is a pointer
Format : data type * Variable name [ The array subscript ]
for example : int *a[4];
explain : Define a name a Pointer array of , Array has 4 Elements , Every element is int * The pointer to

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int *a[4];
	int num1 = 100;
	int num2 = 200;
	int num3 = 300;
	int num4 = 400;
	// Use the pointer array to save the address of the variable 
	// Every element of the pointer array is int * Pointer variable for 
	a[0] = &num1;
	a[1] = &num2;
	a[2] = &num3;
	a[3] = &num4;
	// Traversal pointer array 
	int i;
	for(i = 0 ; i < 4;i++)
	{
		printf("%d ",*a[i]);
	}
	putchar(10);

	char *p[4];
	char s1[] = "hello world";
	char s2[] = "hello beijing";
	char s3[] = "hahahah";
	char s4[] = "nihao nanjing";
	p[0] = s1;
	p[1] = s2;
	p[2] = s3;
	p[3] = s4;
	for(i = 0 ; i < 4;i++)
	{
		printf("p[%d] =%s\n",i,p[i]);
	}
	return 0;
}

Four 、 Pointers and strings

#include <stdio.h>

int main(int argc, const char *argv[])
{
	char s1[] = "hello world";
	char s2[] = "hello world";
	printf("s1 = %p\ns2 = %p\n",s1,s2);
	
	char *p1 = "hello world";
	char *p2 = "hello world";
	printf("p1 = %p\np1 = %p\n",p1,p2);

	//p1[5] = '8';
	//printf("p1 = %s\n",p1);
	p1 = "www.baidu.com";
	printf("p1 = %p %s\n",p1,p1);

	return 0;
}

5、 ... and 、 Multi level pointer

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a = 100;
	// The first level pointer saves the address of ordinary variables 
	int *p = &a;
	// The secondary pointer saves the address of the primary pointer 
	int **q = &p;
	// The third level pointer saves the address of the second level pointer 
	int ***w = &q;

	printf("a = %d %d %d %d\n",a,*p,**q,***w);
	printf("a = %p %p %p %p\n",&a,p,*q,**w);
	
	printf("p = %p %p %p\n",&p,q,*w);
	printf("q = %p %p\n",&q,w);
	return 0;
}

6、 ... and 、const key word

6.1 Global and local variables

#include <stdio.h>
// Our memory is divided into many areas ,
// When defining variables, they will be placed in a specific area according to the defined location and storage type 
// To put it simply : Global area , Static zone , The stack area , Heap area , The constant area , Code area, etc 
//
// Variables defined outside the function are called global variables , Stored in the global area of memory 
// Global variables can be anywhere in the current code ( Main function or sub function ) Use 
int num = 100;
// If global variables are not initialized , Automatically initialized to 0
int a;
int myval = 999;
int main(int argc, const char *argv[])
{
	printf("num = %d\n",num);
	num = 888;
	printf("num = %d\n",num);
	printf("a = %d\n",a);
	// Variables defined inside a function are called local variables , If you don't use storage type decoration , Then the current variable is stored in the stack area 
	// If the space of the stack area is not initialized , It stores random values 
	int b;
	printf("b = %d\n",b);

	printf("myval = %d\n",myval);
	int myval = 888;
	printf("myval = %d\n",myval);
	return 0;
}

6.2 const Modify global variables and local variables

const Keywords mainly mean read-only , But it also needs to be separated and combined

#include <stdio.h>
//const Modify global variable , Only the value of this variable can be used , Nothing can change 
const int num = 100;
int main(int argc, const char *argv[])
{
	const int myval = 111;
	//int *p = &num;

	//*p = 111;
	//printf("*p = %d\n",num);
	//myval = 222;
	//printf("myval = %d\n",myval);
	//const If you modify a local variable , You cannot change the value directly through variables , But you can modify the value by saving the address through variables .
	int *q = &myval;
	*q = 222;
	printf("myval = %d\n",myval);
	printf("myval = %p q = %p\n",&myval,q);
	return 0;
}

6.3 const Modify pointer variables and modify the types of pointer variables

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a = 100;
	//int *p = &a;
	//const It modifies the type of pointer variable , The content of the address pointed to cannot be modified through the pointer variable 
	//const int *p = &a;
	//const Modification is   Pointer to the variable p,
	// The pointer cannot be modified 
	int *const p = &a;
	//const It modifies the type of pointer variable , The content of the address pointed to cannot be modified through the pointer variable 
	//int const *p = &a;
	//const Modify both the type of pointer variable and pointer variable ,
	// be p And p The value of the memory pointed to cannot be modified 
	//const int * const p = &a;
	
	//*p = 300;
	int b = 400;
	p = &b;
	return 0;
}

7、 ... and 、 Storage type

auto
register
static
extern

7.1 auto

When defining variables by default , If the storage type is not added , Generally, it means a local variable
for example :
auto int num = 100;
int a = 100;

7.2 register

register Decorated variables may open up areas in register space , In this way, the operation efficiency will be greatly improved
Register space may not be available , The application did not arrive , It will automatically become auto modification

#include <stdio.h>

int main(int argc, const char *argv[])
{
    register int i,j;
	for(i = 1;i <=10000;i++)
	{
		for(j = 1;j <= 10000;j++)
		{

		}
	}
	return 0;
}

7.3 static

#include <stdio.h>
int main(int argc, const char *argv[])
{
	//static Decorated variables are stored in the static area 
	// If it is not initialized, it will be automatically assigned as 0
	static int num;
	printf("num = %d\n",num);
	return 0;
}

7.4 extern

extern External reference , use extern Decorated variables can be used in other files , Global variables defined in a file can be used in the current file extern Get the specified variable , The value of the corresponding variable can be used

8、 ... and function

8.1 Related concepts of functions

 Function is to put a pile of code to be executed in a code block example , Then give this code a name ,, As long as the name is used, it is equivalent to executing the code block inside , So the function solves the problem of code reusability .
1. Find the entry address of the function through the function name ( The function name is the address )
2. Allocate space for formal parameters 
3. The ginseng , Pass value ( Pass the value of the argument to the value of the formal parameter )( Value passed , Address delivery )
4. Execute function body 
5. The result is returned to the place called 
6. Release space ( Stack space )

8.2 Define a function and its call

8.2.1 Function definition and call

#include <stdio.h>
// General functions are defined in main The exterior of a function , Convenient for other programs to call 
//myfun: Function name , Is an identifier , Meet the naming rules of identifiers 
//void: return type , If there is no return value , Write void
//(): Inside is the parameter to be passed , If there are no parameters , Don't write , however () There has to be 
void myfun()
{
	printf("----------------\n");
	printf("--helloworld----\n");
	printf("-----nihao beijing----\n");
	printf("--------nihao  nanjing--------\n");
	return;
}
int main(int argc, const char *argv[])
{
	// Call a function without parameters and return values 
	// Call syntax : Function name ( Parameters )
	// Be careful : The return value type must not be added when calling 
	// If there are no parameters, you can not pass , Parameters must be passed 
	// No parameters , Function name () Must add 
	myfun();
	myfun();
	myfun();
	myfun();
	myfun();
	myfun();
	myfun();
	return 0;
}

8.2.2 Declaration of functions

#include <stdio.h>
// Declaration of functions 
void myfun();
int main(int argc, const char *argv[])
{
	myfun();
	myfun();
	myfun();
	return 0;
}
void myfun()
{
	printf("----------------\n");
	printf("--helloworld----\n");
	printf("-----nihao beijing----\n");
	printf("--------nihao  nanjing--------\n");
	return;
}

8.2.3 Function registration calls other functions

#include <stdio.h>
// Declaration of functions 
void myfun();
void myfun1();
void myfun2();
void myfun3();
void myfun4();

int main(int argc, const char *argv[])
{
	myfun4();
	return 0;
}
void myfun()
{
	printf("----------------\n");
	printf("--helloworld----\n");
	printf("-----nihao beijing----\n");
	printf("--------nihao  nanjing--------\n");
	return;
}
void myfun1()
{
	printf("11111111\n");
	myfun();
	return;
}
void myfun2()
{
	printf("22222222\n");
	myfun1();
	return;
}
void myfun3()
{
	printf("3333333333333\n");
	myfun2();
	return;
}
void myfun4()
{
	myfun3();
	return;
}

practice : Realization mystrcmp Function functions

#include <stdio.h>
int Mystrcmp(const char *p,const char *q)
{
	while(*p != '\0' && *q != '\0')
	{
		if(*p > *q)
		{
			return 1;
		}
		else if(*p < *q)
		{
			return -1;
		}
		p++;
		q++;
	}
	if(*p == '\0' && *q != '\0')
	{
		return -1;
	}
	else if(*p != '\0' && *q == '\0')
	{
		return 1;
	}
	else if(*p == '\0' && *q == '\0')
	{
		return 0;
	}

}
int main(int argc, const char *argv[])
{
	char s1[] = "hellworld";
	char s2[] = "hello z";
	int ret = Mystrcmp(s1,s2);
	if(ret == 0)
	{
		printf("s1 = s2\n");
	}
	else if(ret > 0)
	{
		printf("s1 > s2\n");
	}
	else
	{
		printf("s1 < s2\n");
	}
	return 0;

}

8.3 The parameters of the function

#include <stdio.h>
int i = 100;
int j = 200;
void myadd1();  
void myadd2();

void myadd3(int a,int b);
int main(int argc, const char *argv[])
{
	myadd1();
	myadd2();
	myadd3(1,2);
	return 0;
}

void myadd1()
{
	int sum = i+j;
	printf("sum = %d\n",sum);
	return;
	//return 1;
}
void myadd2()
{
	i = 300;
	j = 400;
	return;
}
void myadd3(int a,int b)   // Shape parameter , Parameters are separated by commas 
{
	int sum = a + b;
	printf("sum = %d\n",a+b);
}

8.4 The return value of the function

#include <stdio.h>
int add(int a,int b);
int main(int argc, const char *argv[])
{
	int a = 1,b = 2; // Actual parameters 
	int ret = add(a,b);
	return 0;
}
// To the left of the function name is the return value type , Be consistent with the type of the return value 
int add(int a,int b)
{
	int sum = a+b;
	// The return value of the function : At the end of tax inclusive execution , If you need to return some values 
	// Give other functions , Then you need to have a return value , On the contrary, there is no need to return a value 
	return sum;  // Return the result 
}

practice :

Write a function , return a Of n The next power

#include <stdio.h>

int myPow(int a,int n);
int main(int argc, const char *argv[])
{
	printf("%d\n",myPow(3,5));
	return 0;
}
int myPow(int a,int n)
{
	if(a == 0)
	{     
		return 0;
	}
	int num = 1;
	while(n != 0)
	{
		num = num *a;
		n--;
	}
	return num;
}

8.5 Parameter passing method of function

8.5.1 How to pass parameters

There are generally three kinds of :
Global variables pass parameters :
Generally do not use , Because the global variable itself can be used directly inside the function , There is no need for special reference
It's worth passing on : Pass the value of the argument to the formal parameter
Address reference : Pass the address of the argument to the formal parameter

#include <stdio.h>

void Swap(int *a,int *b);
int main(int argc, const char *argv[])
{
	int a = 100,b = 200;
	printf("a = %d,b = %d\n",a,b);
	Swap(&a,&b);
	printf("a = %d,b = %d\n",a,b);
	return 0;
}
void Swap(int *a,int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

8.5.2 One dimensional array parameters

#include <stdio.h>
// One dimensional array is passed to the function as a parameter , There are two ways 
// The first one is : Pass on int a[]
// The second kind : Pass on  int *a  // Commonly used 
// The third kind of : Pass on  int a[100]
//void myOrder(int *arr,int n)
//void myOrder(int arr[],int n)
void myOrder(int arr[1000],int n)
{
	int i;
	printf("sizeof(arr) = %ld\n",sizeof(arr));//arr Just a pointer variable , It's not an array name , Therefore, the space occupied by the array cannot be obtained through this pointer variable 
	for(i = 0 ; i < n;i++)
	{
		printf("%d ",arr[i]);
	}
	putchar(10);

}
int main(int argc, const char *argv[])
{
	int a[] = {1,2,3,4,5,6,7,8,9,10};

	myOrder(a,sizeof(a)/sizeof(a[0]));
	return 0;
}

practice : Write function , Implement bubble sorting

#include <stdio.h>

void Sort(int a[],int length)
{
	int i,j;
	for(i = 0 ; i < length - 1;i++)
	{
		for(j = 0 ; j < length - 1 - i;j++)
		{

			if(a[j] < a[j+1])
			{
#if 0
				int t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
#endif
			a[j] = a[j] + a[j + 1];
			a[j + 1] = a[j] - a[j + 1];
			a[j] = a[j] - a[j + 1];
			}
		}
	}
	
}
void Print(int a[],int length)
{
	int i;
	for(i = 0 ; i < length;i++)
	{
		printf("%d ",a[i]);
	}
	putchar(10);

}
int main(int argc, const char *argv[])
{
	int a[10] = {0};
	printf(" Please enter 10 A digital :\n");
	int i;
	int length = sizeof(a)/sizeof(a[0]);
	for(i = 0 ; i < length;i++)
	{
		scanf("%d",&a[i]);
	}
	Sort(a,length);
	Print(a,length);
	return 0;
}

8.5.3 Pass parameters of two-dimensional array

#include <stdio.h>
// Two dimensional array parameters 
// Method 1: Direct transmission int a[3][4]
// Method 2: Pass array pointer 
void ArrayOrder(int a[3][4])
{
	int i,j;
	for(i = 0 ; i < 3;i++)
	{
		for(j = 0 ; j < 4;j++)
		{
			printf("%-5d",a[i][j]);
		}
		putchar(10);
	}
}
int main(int argc, const char *argv[])
{
	int a[3][4] = {1,2,3,4,5,6,7,8,9,10,12,11};
	ArrayOrder(a);
	return 0;
}

8.5.4 Pointer array pass parameter

#include <stdio.h>

// Pointer array passing :
// The way 1: Pass the pointer array directly 
// The way 2:  Pass the secondary pointer 
void PointArrayOrder(char *s[],int n)
{
	int i;
	for(i = 0 ; i < n;i++)
	{
		printf("s[%d] = %s\n",i,s[i]);
	}
}

int main(int argc, const char *argv[])
{
	char *s[4];
	char s1[] = "hello world";
	char s2[] = "hello nanjing";
	char s3[] = "hello beijing";
	char s4[] = "hello shanghai";
	s[0] = s1;
	s[1] = s2;
	s[2] = s3;
	s[3] = s4;
	PointArrayOrder(s,4);
	return 0;
}

8.6 main Parameters of

#include <stdio.h>
//main The parameters of can be appreciated , There can be only one argc, There can also be two argc and argv,
//argc:int Type variable , The number of parameters used to save the command line 
//argv: It's an array of Pointers , Used to save multiple strings , Used to save every string passed in from the command line 
int main(int argc, const char *argv[])
{
	printf("argc = %d\n",argc);
	int i;
	for(i = 0; i < argc;i++)
	{
		printf("argv[i] = %s\n",argv[i]);
	}
	return 0;
}

8.7 Pointer function

Pointer function : The essence is a function , The return value of the function is an address

#include <stdio.h>
#include <string.h>
// The return value is a function of a pointer , Called pointer function 
char *find_sub(char *str,const char *sub)
{
	int str_len = strlen(str);
	int sub_len = strlen(sub);
	int i;
	for(i = 0 ; i < str_len - sub_len + 1 ;i++)
	{
		if(strncmp(str + i,sub,sub_len) == 0)
		{
			return str + i;
		}
	}
	return NULL;
}
int main(int argc, const char *argv[])
{
	char str[] = "helloworld";
	char sub[] = "worl";
	
	char *s = find_sub(str,sub);
	if(NULL == s)
	{
		printf(" non-existent !\n");
		return -1;
	}
	printf("s = %s\n",s);
	return 0;
}

8.7.1 malloc/free

 The header file :#include <stdlib.h>
 Prototype :void *malloc(size_t size);
      void free(void *ptr);
 function :malloc For manual application on the heap size Bytes of space 
     free For manual release malloc Space to apply 
 Parameters :(malloc):size To apply for size Bytes 
      (free):ptr: Indicates the first address of the application space 
 Return value :(malloc):
		 success : Return the first address of the application space , The type is void*( Universal pointer )
		 Failure : return NULL
      	(free): No return value 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// The return value is a function of a pointer , Called pointer function 
char *find_sub(char *str,const char *sub)
{
	int str_len = strlen(str);
	int sub_len = strlen(sub);
	int i;
	for(i = 0 ; i < str_len - sub_len + 1 ;i++)
	{
		if(strncmp(str + i,sub,sub_len) == 0)
		{
			return str + i;
		}
	}
	return NULL;
}
void InitMemory(char **str,char **sub)
{
	*str = (char *)malloc(sizeof(char)*128);
	*sub = (char *)malloc(sizeof(char)*32);
}

int main(int argc, const char *argv[])
{
	//char str[] = "helloworld";
	//char sub[] = "worl";
	char *str = NULL,*sub = NULL;
	InitMemory(&str,&sub);
	scanf("%s%s",str,sub);
	char *s = find_sub(str,sub);
	if(NULL == s)
	{
		printf(" non-existent !\n");
		return -1;
	}
	printf("s = %s\n",s);
	free(str);
	free(sub);
	str = NULL;
	sub = NULL;

	return 0;
}

Homework : All code must be typed at least 2 All over

原网站

版权声明
本文为[Light chasing rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130551477894.html