Fourth Training of C CP Language Group - Array+Pointer

1 Array

NO.1 One-Dimensional Array

I. Integrated initialization of arrays

1,int a[]={2,4,6,8,9,1,0,11,44,9}
(1) Give the initial values of all elements of the array directly in curly braces
(2) No need to give the size of the array, the compiler counts for you

2,int b[20] = {2}
If the size of the array is given, but the number of subsequent initial values is insufficient, then the subsequent elements are initialized to zero

2. Positioning at Initialization of Integration

Note: Only C99 is supported

int c[20]={
	[0]=2,[3]=1,8,
}

(1) Give location in initialization data with [n]
(2) Unpositioned data follows the previous position
(3) Value zeros at other locations
(4) You can also let the compiler calculate without giving the size of the array
(5) Arrays that are particularly suitable for sparse initial data

3. Array size


(1) sizeof gives the size of the entire array in bytes
(2) sizeof(a[0]) gives the size of a single element in the array, and divides to get the number of cells in the array
(3) Such code, once the initial data in the array is modified, does not need to modify the traversed code

Array length formula:

int a[20];
int length=sizeof(a)/sizeof(a[0]);//a[0] Any one element will do
printf("%d",length)

4. Assignment of Arrays


Array variables themselves cannot be assigned

//Error example:
int a[10];
int b[10];
a=b;//Is wrong

Note: To pass all elements of one array to another, traversal is required.

5. Traversing Arrays

Traversal:

int a[15];
int length=sizeof(a)/sizeof(a[0]);
int b[15];
for(int i=0;i<length;i++)//Notice that it is less than, not equal to
{
	b[i]=a[i]
}

Note: Next, you should pay attention to the use of variable i.

6. Arrays as parameters of functions

When an array is a function parameter, it is often necessary to pass in the size of the array with another parameter
(1) The size of the array cannot be given in []
(2) You can no longer use sizeof to calculate the number of elements in an array

7. Array example: prime number

Prime number: divisible only by 1 and itself
Determine prime numbers:

#include<stdio.h>
int isPrime(int x)
{
	for(int i=2;i<x;i++)
	{
		if(x%1==0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int a;
	scanf("%d",&a);
	if(isPrime(a)==1)
	{
		printf("prime number");
	}
	else
	{
		printf("Not a prime number");
	}
}

Write the determined prime number into the array and iterate through the output

#include<stdio.h>
int isPrime(int x)
{
	for(int i=2;i<x;i++)
	{
		if(x%1==0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	int count = 0;
	for (int i=a;i<=b;i++)
	{
		if(isPrime(i)==1)
		{
			c[count]=i;
			count++;
		}
	}
	for (int i=0;i<count;i++)
	{
		printf("%d",c[i]);
	}
}

NO.2 sqrt function (open root)

function library
#include<math.h>
Application:

#include<math.h>
int main()
{
int a=100;
a=sqrt(a);
printf("%d",a);
}

NO.3 2-D Array

1. Definition of two-dimensional arrays

int a[3][5]: is generally understood as a 3-row, 5-column matrix (see figure)

2. Two-dimensional array traversal

Two for loops
First for loop: how many lines to control
Second for loop: how many columns to control

int a[3][5];
for(int i=0;i<3;i++)
{
	for(int j=0;j<5;j++)
	{
		a[i][j] = i*j;
	}
}

3. Initialization of two-dimensional arrays

(1) The number of rows may not be given first, but must be given by the compiler.
(2) One {} per line, separated by commas
(3) The last comma may or may not exist
(4) If the array content is omitted, it means zero completion
(5) Location is also available (only C99 is available)

int a[][5]={
					{1,2,3,4},  //Notice the comma
					{1,2,3,4}   //Is the last line comma okay or not 
				};

(2) Address

1. Operators&

&: To get the address of a variable, its operand must be a variable

int a=0;
scanf("%d:",&a); //&is the address for a
printf("%d",&a);//Print a's address//decimal address
printf("%p",&a);//Print hexadecimal address (complete)
printf("%x",&a);//Print hexadecimal addresses (omit 0)

int i;print("%p",&i);
Note: Whether the address size is the same as int depends on the compiler

2. &Undesirable address

&(a+b)
&(a++)
&(++a)
Note: The brackets are not variables anymore, they are data.

3. Special &

(1) Address of adjacent variables

#include<stdio.h>
int main ()
{
	int a=1;
	int b=2;
	int c=3;
	printf("%d %d %d",&a,&b,&c);
	return 0;
}


(2) & the result of sizeof

#include<stdio.h>
int main ()
{
	int a;
	printf("%d",sizeof(&a));
	return 0;
}


(3) Address of the array
(4) Address of array unit
(5) Addresses of adjacent array cells

int a[10];
printf("%x ",&a);
printf("%x ",&a[0]);//Same as above
printf("%x ",&a[1]);//Address difference of adjacent array cells 4

(3) Pointer

1. Pointer Definition

Is the variable that holds the address

#include<stdio.h>
int main()
{
	int * a=NULL;//The pointer must be set to empty at the beginning, note *must have
}
#include<stdio.h>
int main()
{
	int b=50;
	int * a=&b;//Here * is for decoration
	printf("%d",*a);//Remove it with * to represent the address (unlike the * above)

int* p,q: Pointer assigns to global
int *p,q: pointer only assigned to p

2. Pointer Variables

(1) The value of the variable is the memory address
(2) The value of a common variable is the actual value
(3) The value of a pointer variable is the address of a variable of practical significance

3. Pointer as a parameter

void f(int *p);
* Get the address of a variable when adjusted:
int i=0; f(&i);
* This i in function can be accessed through this pointer

4. To access variables at that address, use *

Definition: ✳ Is a set of univariate operators that access the address variable represented by the pointer's value
& (get address) and ✳ (Get the variable on the address)
✳ And & can cancel out

5. Pointer Operator & *

Reciprocal interaction
(1)*&yptr -> * (&yptr) -> * (place of yptr)
Address) ->Get the variable on that address - > yptr
(2) &*yptr -> &(*yptr) -> &(y) -> get y's place
Address, also known as yptr -> yptr

6. The most common errors of the pointer:

A pointer variable is defined, and the pointer is used before pointing to any variable

Keywords: C Algorithm

Added by mlnsharma on Fri, 28 Jan 2022 11:21:46 +0200