C language-learning path-04

Arrays and Strings

Array: In order to facilitate data processing, several variables of the same type are organized in an ordered form.
An array is a contiguous space of variables of the same type in memory. All members of the same array are of the same data type, and the addresses of all members in memory are also consecutive.
insert image description here
Arrays are of constructed data types:

  • An array can be decomposed into array elements; these array elements can be primitive data types or constructed types.
    ( int a[20]; struct Stu boy[10]; )
  • According to the different types of array elements, arrays can be divided into: numeric arrays, character arrays, pointer arrays, and structure arrays.
    ( int a[10]; char s[10]; char *p[10]; )
    In general, the number of subscripts of array elements is also called dimension. According to different dimensions, the array can be divided into one-dimensional Arrays, two-dimensional arrays, three-dimensional arrays, etc. Arrays of two dimensions or more are usually called multidimensional arrays.
one-dimensional array
Definition and use of one-dimensional array
  • Array names should be specified by identifiers (numbers, letters, underscores).
  • The array name cannot be the same as other variables, it is unique within the same scope.
  • The constant expression in [] represents the number of array elements. (Subscript counts from 0)
  • When defining an array, it is better to use a constant in [], and when using an array, the [] can be a constant or a variable.
    As follows:
#include <stdint.h>
int main()
{
    
    
	int a[5];//定义数组a ,内有5个成员,每个成员都是int型数据
	//下标从0开始,a[0]、a[1]、a[2]、...、a[4]
	//没有a这个变量,a只是数组名字,但不是变量,它是常量。
	a[0] = 0;
	a[1] = 1;
	a[4] = 4;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
    
    
		a[i] = i;  //for语句下,给数组a赋值。
	}

	//遍历数组,并输出数组中每个成员的值
	for (i = 0; i < 5; i++)
	{
    
    
		printf("%d\n",a[i]);
	}
	return 0;
}
Initialization of one-dimensional array

Assigning values ​​at the same time as defining the array is called initialization. If the global array is not initialized, the compiler will initialize it to 0. If the local array is not initialized, the content will be a random value.

	int a[5] = {
    
     1,2,3,4,5 }; //初始化数组,并同时赋值
	int a[5] = {
    
     1,2,3 }; //只初始化前三个值,后面的成员默认设置为0
	int a[5] = {
    
     0 }; //所有成员都设置为0
	int a[] = {
    
    1,2,3,4,5}//初始化时,如果[] 没有填写具体值,成员值必须初始化。
array name

The array name is just a constant address, representing the address of the first element in the array.

#include <stdint.h>
int main()
{
    
    
	int a[5] = {
    
     1,2,3,4,5 };

	printf("a=%p\n",a);
	printf("&a[0]=%p\n",&a);

	int n = sizeof(a); //  数组a所占用内存的大小,5个int类型的成员,5*4=20
	int n0 = sizeof(a[0]); // 数组a第一个成员的占用内存大小,即第0个元素,int  =4
	
	int i = 0;
	for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
    
    
		printf("%d\n",a[i]);
	}
	return 0;
}
One-dimensional array exercises
  1. The most value problem in a one-dimensional array:
#include <stdint.h>
int main()
{
    
    
	int a[10] = {
    
     6,1,8,2,9,3,-4,5 ,4,-2};
	int i = 0;
	int max = a[0];
	for (i = 0; i < 10; i++)
	{
    
    
		if (a[i] > max)
		{
    
    
			max = a[i];
		}
	}

	printf("max=%d\n",max);

	return 0;
}
  1. inverse of a one-dimensional array
#include <stdint.h>
int main()
{
    
    
	int a[10] = {
    
     6,1,8,2,9,3,-4,5 ,4,-2};
	int i = 0;
	int j = sizeof(a) / sizeof(a[0]) - 1;
	while (i < j)
	{
    
    
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
		i++;
		j--;
	}

	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
    
    
		printf("%d\n", a[i]);
	}

	return 0;
}
  1. bubble sort
#include <stdint.h>
int main()
{
    
    
	int a[10] = {
    
     6,1,8,2,9,3,-4,5 ,4,-2};
	int i = 0;
	int n = sizeof(a) / sizeof(a[0]) ;
	for (i = 0; i < n; i++)
	{
    
    
		for (int j = 0; j < n - i - 1; j++)
		{
    
    
			if (a[j] > a[j+1])
			{
    
    
				int temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}

	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
    
    
		printf("%d\n", a[i]);
	}

	return 0;
}
Two-dimensional array
Definition and use of two-dimensional array

The general form of a two-dimensional array:
type specifier array name [dimension 1] [dimension 2] Example: int a[2][3]
insert image description here
From the perspective of computer memory, there is actually no two-dimensional array, and it is essentially a one-dimensional array . The two-dimensional array actually puts the data in the first row, and then puts the data in the second row until all of them are put in. Its memory addresses are still continuous!

#include <stdint.h>
int main()
{
    
    
	int a[3][4];
	int num = 0;
	for (int i = 0; i < 3; i++)
	{
    
    
		for (int j = 0; j < 4; j++)
		{
    
    
			a[i][j] = num++;
		}
	}

	for (int i = 0; i < 3; i++)
	{
    
    
		for (int j = 0; j < 4; j++)
		{
    
    
			printf("%d\n",a[i][j]);
		}
	}
	return 0;
}
Initialization of two-dimensional array
#include <stdint.h>
int main()
{
    
    
	//方式一
	int a[3][4] = {
    
     {
    
    1,2,3,4},{
    
    5,6,7,8},{
    
    9,10,11,12} };
	int a[3][4] =
	{
    
    
		{
    
    1,2,3,4},
		{
    
    5,6,7,8},
		{
    
    9,10,11,12},
	};

	//方式二
	int a[3][4] = {
    
     1,2,3,4,5,6,7,8,9,10,11,12 };

	//方式三
	int a[3][4] = {
    
     1,2,3,4,5,6,7, }; //部分赋值初始化,未初始化的,默认为0.

	//方式4
	int a[3][4] = {
    
     0 };//全部为0

	//方式5  //不建议使用!有些编译器会直接报错
	int[][4] = {
    
     1,2,3,4,5,6,7,8 }; //如果存在[]为空,定义时必须初始化。
	return 0;
}
array name

The array name is an address constant, representing the address of the first element in the array.

#include <stdint.h>
int main()
{
    
    	
	//二维数组a
	//二维数组本质上还是一维数组,int a[3][4]  中有3个一维数组,每个 一维数组又有4个元素。
	int a[3][4] = {
    
     {
    
    1,2,3,4},{
    
    5,6,7,8},{
    
    9,10,11,12} };	

	printf("%p\n", a);  //数组名 a是数组首元素的地址,二维数组的第0个元素为一维数组
	printf("%p\n", a[0]);//第0个一维数组的数组名为a[0]

	//使用sizeof计算二维数组所占用的内存空间
	printf("sizeof(a) = %d\n",sizeof(a));

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

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

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

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

	printf("行数*列数=%d\n", sizeof(a) / sizeof(a[0][0]));
	
	return 0;
}
Character arrays and strings

Since there is no string data type in C language, it can be replaced by an array. For example: char a[]
In C language, strings must be common with char arrays, but char arrays are not necessarily strings!
A char array terminated by the number 0 (equivalent to the character '\0') is a string. If the char array does not end with the number 0, then the char array is just an array of characters, not a string!
Not ending with '\0':
insert image description here
Ending with '\0':
insert image description here
Another way:

#include <stdint.h>
int main()
{
    
    	
	char c[] = {
    
    "hello"};
	printf("%s\n", c);
	return 0;
}
String input and output
#include <stdint.h>
int main()
{
    
    	
	char str[100];
	printf("请输入字符串:");
	scanf_s("%s",str); //默认以空格分隔
	printf("字符串:%s",str);
	return 0;
}

random number

To get a random number, first add the header file!

	time_t time(time_t * t);  //获取当前系统时间

	void srand(unsigned int seed);  //用来设置rand()产生随机数时的随机种子

	int rand(void);  //返回一个随机数
#include <stdint.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    
    	
	time_t tm = time(NULL);//获取当前系统时间
	srand((unsigned int)tm);//设置随机种子

	int t = rand();
	printf("t=%d\n", t); //获取随机数
	return 0;

}

insert image description here

string processing functions

Commonly used string processing functions are: (explain later when used)

  • gets();
  • fgets();
  • puts();
  • fputs();
  • strlen();
  • strcpy();
  • strncpy();
  • cracked();
  • strncat();
  • strcmp();
  • strncmp();
  • sprintf();
  • sscanf();
  • strchr();
  • strstr();
  • strtok();
  • tow();

Guess you like

Origin blog.csdn.net/weixin_50918736/article/details/130463861