C language learning notes - P10

catalogue

preface:

  ● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!

Array explanation (1)

  Topic - find the maximum value of 10 integers

Code optimization:

Title - print multiplication table:

  Array pass parameter, pass is the address of the first element! The reasons are as follows:

Address in pointer form:

Examples of global variables:

  array

1, One dimensional array creation and initialization.

  1.2 initialization of array

  Array creation uninitialized is uncontrollable! Better initialize!

1.3 use of one-dimensional array

Print the address of each array element:

Summary:

1.4 storage of one-dimensional array in memory

Comparison of print address forms:

2. Creation and initialization of two-dimensional array

2.1 creation of two-dimensional array

  2.2 initialization of two-dimensional array

  Through the results, we can analyze that in fact, two-dimensional arrays are also continuously stored in memory!

preface:


  ● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!

Array explanation (1)

  Topic - find the maximum value of 10 integers

 //Find the maximum of 10 integers
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int max = 0;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		if(arr[i]>max)
		{
			max = arr[i];
		}
	}
	printf("%d\n", max);

	return 0;
}

Code optimization:

int main()
{
	int arr[] = { -1,-2,-3,-4,-5,-6,-7,-8,-9,-10 };
	int max = arr[0]; //Suppose the first element is the maximum
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	printf("%d\n", max);

	return 0;
}

Solve the specified array element:
int main()
{
	int arr[10] = { 0 };
	//Input value
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		scanf("%d ", &arr[i]);
	}
	int max = arr[0]; //Suppose the first element is the maximum int max=-1 or any other number
	
	for (i = 0; i < 10; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	printf("%d\n", max);

	return 0;
}

Title - print multiplication table:

int main()
{
	int i = 0;
	for (i = 1; i <=9; i++)
	{
		//Print one line
		int j = 0;
		for (j = 1; j <= i; j++)
		{
			printf("%d*%d=%d\t", i, j, i * j);
		}
		printf("\n");
	}
	
	return 0;
}

 

 

int test()
{
	int a = 10;
	int b = 20;
	return a, b;
}
int main()
{
	int ret = test();
	printf("%d\n", ret);
	return 0;
}

  Array pass parameter, pass is the address of the first element! The reasons are as follows:

void test(int arr[])
{
	int a = 10;
	int b = 20;
	arr[0] = a;
	arr[1] = b;
}
int main()
{
	int arr[2] = { 0 };
	//int ret = test();
	test(arr);
	printf("%d %d\n", arr[0],arr[1]);
	return 0;
}

Address in pointer form:

void  test(int *px,int *py)
{
	int a = 10;
	int b = 20;
	*px = a;
	*py = b;
}
int main()
{
	int x = 0;
	int y = 0;
	test(&x, &y);
	printf("%d %d\n", x,y);
	return 0;
}

Examples of global variables:

a,b Is a global variable, test,main Functions can be called and changed, which is not safe

int a = 0;
int b = 0;
void test()
{
	int x = 10;
	int y = 20;
	a = x;
	b = y;
}
int main()
{
	test();
	printf("%d %d\n", a, b);//10 20
	return 0;
}

 

  array

1, One dimensional array creation and initialization.

1.1 creation of array

An array is a collection of elements of the same type.

How to create an array:

type_t   arr_name   [const_n];
//type_t is the element type of the index group
//const_n is a constant expression that specifies the size of the array

  Instance of array creation:

//Code 1
int arr1[10];
//Code 2
int count = 10;
int arr2[count];//When can arrays be created normally?
//Code 3
char arr3[10];
float arr4[1];
double arr5[20];

 

 

  1.2 initialization of array

Array initialization refers to giving the contents of the array some reasonable initial values (initialization) while creating the array.

give an example:

int arr1[10] = {1,2,3};    Incomplete initialization. The default initialization value of the remaining elements is 0
int arr2[] = {1,2,3,4};    If the array size is not specified, the array size is determined by the initial
int arr3[5] = {1,2,3,4,5};
char arr4[3] = {'a',98, 'c'}; 98 Is character b of ASCII Code value
char arr5[] = {'a','b','c'};
char arr6[] = "abcdef";    String initialization character array

 

 

 

 

 

  Array creation uninitialized is uncontrollable! Better initialize!

1.3 use of one-dimensional array

For the use of arrays, we introduced an operator: [], subscript reference operator. It's actually an array access operator.

#include <stdio.h>
int main()
{
    int arr[10] = { 0 };//Incomplete initialization of array
       //Count the number of elements in the array
    int sz = sizeof(arr) / sizeof(arr[0]);
    //Assign a value to the contents of the array. The array is accessed by subscript, which starts from 0. So:
    int i = 0;//Subscript
    for (i = 0; i < 10; i++)//Write 10 here, okay? i<sz
    {
        arr[i] = i;
    }
    //Output the contents of the array
    for (i = 0; i < 10; ++i)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

 

Print the address of each array element:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//Print the address of each element
	int i = 0;
	for (i -= 0; i < 10; i++)
	{
		printf("&arr[%d]=%p\n", i, &arr[i]); %p-Print address
	}
	return 0;
}

 

Summary:

1. One dimensional arrays are stored continuously in memory

2. As the subscript of the array increases, the address changes from low to high

1.4 storage of one-dimensional array in memory

Carefully observe the output results. We know that with the increase of array subscripts, the address of elements is also increasing regularly. It can be concluded that arrays are stored continuously in memory.

Comparison of print address forms:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//Print the address of each element
	int *p =&arr[0];
	int i = 0;
	for (i -= 0; i < 10; i++)
	{
		//printf("%p\n", p+i);  //p+i is the address of arr[i] in the array
		printf("%p-----%p\n", p + i, &arr[i]);
	}
	return 0;
}

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//Print the address of each element
	int *p =&arr[0];
	int i = 0;
	for (i -= 0; i < 10; i++)
	{
		//printf("%p\n", p+i);  //p+i is the address of arr[i] in the array
		//printf("%p-----%p\n", p + i, &arr[i]);
		printf("%d", *(p + i));
	}
	return 0;
}

2. Creation and initialization of two-dimensional array

2.1 creation of two-dimensional array

//Array creation
int arr[3][4];
char arr[3][5];
double arr[2][4];

  2.2 initialization of two-dimensional array

Two dimensional array initialization example:

int main()
{
	int arr[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	return 0;
}

int main()
{
	int arr[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	//int arr[3][4] = { 1,2,3,4,5,6,7 };
	//int arr[3][4] = { {1,2},{3,4},{5,6},7};
	//printf("%d\n", arr[2][3]);
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 4; j++)  //column
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
	return 0;
}

 

 

 

int main()
{
	int arr[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
		{
			printf("&arr[%d][%d]=%p\n", i, j, &arr[i][j]);
		}
	}
	return 0;
}

 

  Through the results, we can analyze that in fact, two-dimensional arrays are also continuously stored in memory!

The array learning notes are continuously updated

Tags: C

Posted on Sat, 06 Nov 2021 14:40:24 -0400 by sargus