2020-11-29 Second weekly report

2020-11-28 Second Weekly Report

——————————————————————————————
Xiaocaiji will share what he learned this week again this time. The focus of what he learned this week is still Put it on the C language.

  1. Global variables and local variables
    Global variables: variables defined outside all functions. The scope is from the beginning of the definition to the result of the program.
    Local variables: formal parameters or variables defined inside a function. Can only be used within this function.
    But it should be noted that if the name of the local variable defined in a program is the same as the name of the global variable, the local variable will shield the global variable . The following program will output '8'.
#include <stdio.h>
int i=99;
void f(int)
{
    
    
printf("%d",i);
}
int main()
{
    
    
	f(8);
	return 0;
}
  1. Pointer
    Pointer: The essence is the address, that is, the number of each memory unit.
    The advantages of pointers: can express some data structures, quickly transfer data, make functions return more than one value, can directly access the hardware, and can conveniently process strings.
    Personal understanding is all in the following program.
#include <stdio.h>
int main()
{
    
    
	int * p;//p是变量名字,int * 表示变量p存放的是int类型的变量的地址
	int i=3;//
	p=&i;//使p保存了i的地址,因此p指向i,但p不是i。
	*p=i;//*p=i(* 指针变量)=(普通变量)。
	printf("%d",*p);
	return 0;
}

Pointer realizes the exchange of two numbers

#include <stdio.h>
void exchange( int * p, int * q)
{
    
    
	int t;
	t=*p;
	*p=*q;
	*q=*p;
}
int main()
{
    
    
	int a=3;
	int b=5;
	f(&a,&b);
	printf("%d %d",a,b);
	return 0;
}

The pointer outputs two variables of a one-dimensional array: the first address of the array and the length of the array.

#include <stdio.h>
void f(int * p,int len)
{
    
    
	p[3]=88;
}
int main(void)
{
    
    
	int a[6]={
    
    1,2,3,4,5,6};
	printf("%d\n",a[3]);
	f(a,6);//首地址+长度
	printf("%d\n",a[3]);
	return 0;
}

Tips: In general, pointer variables cannot be added, subtracted, multiplied and divided. Only when two pointer variables point to different storage in the same continuous space, pointer variables can be subtracted.
The number of bytes occupied by all pointer variables is 4 bytes.

3. Dynamic memory allocation

  1. Must use malloc function.
  2. malloc has only one parameter, and the parameter is plastic.
  3. The formal parameter (4) means to request the system to allocate 4 bytes for this program
  4. Format such as int * p=(int*) malloc(4);

Dynamically allocated array

#include <stdio.h>
int main()
{
    
    
	int a[5];
	int len;
	int * p;
	int i;
	printf("请输入你想要的长度");
	scanf("%d",&len);
	p=(int *)malloc(4*len);
	for(i=0;i<len;i++)
	{
    
    
			printf("%d\n",p[i]);
	}
	free(p); //释放点动态分配的数组
	return 0;
}


多级指针

```c
#include <stdio.h>
int main()
{
    
    
	int i=10;
	int * p=&i;
	int **q=&p;
	int ***r=&q;
	printf("%d",***r);
	return 0;
	
}

4. Structure
Why do I need a structure?
In order to express a complicated thing.
What is a structure?
A new composite data type formed by combining some basic data types.

#include <stdio.h>
struct student
{
    
    
	int score;
	char sex;
};
int main()
{
    
    
	struct student st={
    
    80,'f'};
	printf("%d %c",st.score,st.sex);
	return 0;
}

5. Enumeration
Definition: will matter

#include <stdio.h>
enum weekday
{
    
    
	Monday,Turday,Wednesday,Tuesday,Friday,Saturday,Sunday
};
int main()
{
    
    
	enum Weekday.day=Wednesday;
	printf("%d\n",day);
	return 0;
}

6. Student achievement system
pointer + structure + bubble sorting
(dynamic allocation of memory + pointer one-dimensional array)

#include <stdio.h>
#include <malloc.h>

struct student
{
    
    
	int score;
	char name[100];
	//char name[100];

};

int main ()
{
    
    
	int i,n,j;
	struct student * f;
	struct student t;
	printf("你要输入学生的个数\n");
	scanf("%d",&n);
	
	f=(struct student *)malloc(n*sizeof(struct student));//动态创造一维数组!!!!
	for(i=0;i<n;i++)
	{
    
    
		printf("请输入第%d位同学的成绩:\n",i+1);
	
		scanf("%d",&f[i].score);

		scanf("%s",f[i].name);//本身就是数组名不需要加&符号

		//scanf("%s",f[i].name);//本身就是数组名
		
	}
	for(i=0;i<n-1;i++)//指针数组的交换
	{
    
    
		for(j=0;j<n-i-1;j++)
		{
    
    
			if(f[j].score>f[j+1].score)
			{
    
    
				t=f[j];
				f[j]=f[j+1];
				f[j+1]=t;
			}
		}
	}
	for(i=0;i<n;i++)//结构体的输出
	{
    
    
		
		
		printf("score=%d\n",f[i].score);
		printf("sex=%s\n",f[i].name);
		//printf("name=%s\n",f[i].name);

	}


	return 0;
}



That's it for sharing, and the child will continue to cheer.

Guess you like

Origin blog.csdn.net/m0_52699073/article/details/110292080