C tutorial ------ > on the sixth day, the secondary pointer, the relationship between pointer and array, and the position sorting

Posted by simrx11 on Fri, 28 Jan 2022 06:04:31 +0100

1. Secondary pointer:


Two dimensional array Array of arrays
Secondary pointer? --- " Pointer to pointer

Concept: the secondary pointer stores the address of the primary pointer
(definition means space)
If there is space, there will be an address)
  
Format:
Define the format of the first level pointer:
Storage type * data type * pointer variable name;
       
Define the format of the secondary pointer:
Storage type * * data type * * pointer variable name;
Analysis:
Storage type: the storage type of the secondary pointer itself
Data type * *: the data type of the secondary pointer
Data type *: the type pointed to by the secondary pointer
Pointer variable name: see the meaning of name
            
--------------------------------------------------------
Summary:
(1) When is it necessary to define a secondary pointer When the value of this first level pointer is required
(2) How to determine the type of pointer?
-----Remove [variable name], and the rest is the type of the pointer itself
     eg:
         int *p; int *
         int **pp; int **
(3) How to determine the type pointed to by the pointer?
----"[* pointer variable name] is removed, and the rest is the type pointed to by the pointer
(4) How to analyze the size of a pointer's one-time access space?
----"> depends on the type pointed to
    char **pp; pp the size of one-time access space is 4
    char *p; p one time access space is 1
    int ****pppp;pppp can access 4 spaces at one time
    

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a = 90;
	int b = 78;
	int *p = &a;
	printf("*p = %d\n",*p);
	//*p = 56;
	//p = &b;
	int **pp = &p;
	*pp = &b;
	printf("*p = %d\n",*p);
	
	return 0;
}


-----------------------------------------------------------------------------------------------------

Combination of pointer and array:
Relationship between pointer and one-dimensional array:

Arithmetic operation of pointer: (+ - * /% + + -)

Let's take p and q as examples (Note: p and q are pointer variables of the same type)
p + N: represents the size of N data types moved by p in the direction of address increase (p + sizeof (data type) * n)
p - N: represents the size of N data types moved by p in the direction of address reduction (p - sizeof (data type) * n)
p + +: represents the size of one data type moved by p in the direction of address increase (p + sizeof (data type) * 1)
p --: represents the size of p moving 1 data type in the direction of address reduction (p - sizeof (data type) * 1)
p-q: represents the number of elements separated between two pointers (P - Q / sizeof (data type))

#include <stdio.h>

int main(int argc, const char *argv[])
{
	//Define three pointer variables
	int *p;
	short *q;
	char *s;
	int *r = p+4;


	printf("r-p = %d\n",r-p);
	/*

	p++;
	printf("p = %p\n",p);
	printf("p = %p\t p + 1 = %p\n",p,p+3);
	printf("q = %p\t s + 1 = %p\n",q,q+1);
	printf("s = %p\t s + 1 = %p\n",s,s+1);
	*/
	return 0;
}


The difference between p + + and p+1:
p++<===> p = p+1; Therefore, P + + will cause the pointer to change
p+1 does not cause the pointer to change

Array name:
(1) As an array name, it can represent the whole array
(2) It can also represent the first address of the array


How to print out array elements:
arr[i]  <===> *(arr+i)  <===> *(p+i) <===> *(p++)  <===> p[i] <===> i[arr]  <===> i[p]

​
#include <stdio.h>

int main(int argc, const char *argv[])
{
	int arr[5] = {1,2,3,4,5};
	printf("sizeof(arr) = %d\n",sizeof(arr));
	//Define a pointer to the first element of the one-dimensional array
	int *p = arr;

	//printf("arr = %p\n",arr);
	//printf("&arr[0] = %p\n",&arr[0]);
	int i;
	for(i=0;i<5;i++)
	{
		//printf("%d ",arr[i]);
		//printf("%d ",*(arr+i));
		//printf("%d ",*(p+i));
		//printf("%d ",*(p++));
		//printf("%d ",p[i]);
		//printf("%d ",i[arr]);
		printf("%d ",i[p]);
	}
	printf("\n");
	return 0;
}

​


Analysis: the reason why we can use p[i] to traverse the array is that the compiler will automatically compile p[i] into * (p+i)

Summary:
For array names: arr[i]
For pointers: * (p+i) / *(p + +)


Exercise:
(1) Get a string from the keyboard and use the pointer to find the number of spaces in the string

​
#include <stdio.h>


#define N 20
int main(int argc, const char *argv[])
{
	//Define a character array
	char str[N] = {'\0'};
	//Defines a character pointer to the first element of the array
	char *p = str;
	printf("Please enter a string:\n");
	gets(p);

	int count = 0;
	//Traverse the string
	while(*p != '\0')
	{
		if(' ' == *p)
		{
			count++;
		}
		p++;
	}
	printf("count = %d\n",count);
	return 0;
}

​


(2) Use pointer to realize bubbling (change pointer pointing / do not change pointer pointing)

#include <stdio.h>

#define N 5
 int main(int argc, const char *argv[])
 {
 	//Sorting using pointers
	int arr[N] = {0};
	//Defines an integer pointer variable that points to the first element of the array
	int *p = arr;

	printf("Please enter:\n");
	int i,j;
	for(i=0;i<N;i++)
	{
		//scanf("%d",&arr[i]);
		scanf("%d",p+i);
	}

	printf("Sort before: \n");
	for(i=0;i<N;i++)
	{
		//printf("% ",arr[i]);
		printf("%d ",*(p+i));
	}
	printf("\n");


	for(i=0;i<N-1;i++)
	{
		p = arr;//Let p point back to the first element
		for(j=0;j<N-1-i;j++)
		{
			if(*p > *(p+1))
			{
				int temp;
				temp = *p;
				*p = *(p+1);
				*(p+1) = temp;
			}
			p++;
		}
	}
	printf("After sorting is: \n");
	p = arr;
	for(i=0;i<N;i++)
	{
		//printf("% ",arr[i]);
		printf("%d ",*(p+i));
	}
	printf("\n");
	
	


 	return 0;
 }

Bubble sorting: compare from left to right

Note: find the location of bubbles according to the data
Select sort:
Suppose there are N numbers:
First trip: find the maximum number and exchange it with the number with position 0
The second trip: subtract 1 from the total number, and then find the largest clock to exchange with the number with position 1..
         . . . . . . . . . . . .
         


Note: select to find data by location

#include <stdio.h>

#define M 5
int main(int argc, const char *argv[])
{
	int arr[M] = {0};

	int i;
	for(i=0;i<M;i++)
	{
		scanf("%d",&arr[i]);
	}

	int j;
	//int index;
	//Select sort
	for(i=0;i<M-1;i++)
	{
		//index = i;
		for(j=i+1;j<M;j++)
		{
			/*if(arr[index] > arr[j])
			{
				index = j;
			}
			*/
			if(arr[i] > arr[j])
			{
				int temp;
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
		/*
		//exchange
		int temp;
		temp = arr[i];
		arr[i] = arr[index];
		arr[index] = temp;
		*/
	}

	printf("After sorting is:\n");
	for(i=0;i<M;i++)
	{
		printf("%d ",arr[i]);
	}
	putchar('\n');
	return 0;
}


Job:
1. Pointer implementation, define a string, and realize the inversion of string "break" -- > "kaerb"

#include<stdio.h>
#include<string.h>

#define M 5
int main(int argc, const char *argv[])
{
	//Pointer implementation, string inversion
	char arr[M] = {'\0'};
	printf("Please enter a string:\n");

	//Define pointer
	char *p = arr;	//	p = &arr[0]
	gets(p);
	
	//Use strlen to measure the effective bytes of the arr array
	int l;
	l = strlen(arr);

	//P (first address) + l -1 is the last address
	p = p + l - 1;

	int i;
	for(i = 0;i < M;i++)
	{
		printf("%c",*(p-i));
		
	}
	putchar('\n');

	return 0;
}


2. Use pointer to realize strcat function

#include<stdio.h>
#include<string.h>

#define M 15
#define N 20
char *mystrcat(char *p,char*q,int n)
{
	if(NULL == p || NULL == q)
	{
		printf("NULL_ERROR!\n");
		return NULL;
	}

	while(*p)
	{
		p++;
	}

	//N stands for n bits connecting arr2
	while(n--)
	{
		*p++ = *q++;
	}

	*p = '\0';
	return q;
}
int main(int argc, const char *argv[])
{
	//Use pointer to realize the first n bits of strcat
	char arr1[M] = {'\0'};
	char arr2[N] = {'\0'};
	printf("Please enter two strings:\n");

	//gets input string
	gets(arr1);
	gets(arr2);
		
	/*Defines a pointer to the first address of the array
	char *p= &arr1[d];
	char *q= &arr2[0];*/

	//Defines the valid character length of arrays arr1 and arr2
	int l1,l2;
	l1 = strlen(arr1);
	l2 = strlen(arr2);
	
    if(	M < l1 + l2 + 1)
	{
			printf("error!!!\n");
			return -1;
	}
	
	/*while(*p)
	{
		p++;
	}*/

	/*Traversal arr2
	while(*q)
	{
		
		*p++ = *q++; 
	}*/
	char *s;
	s = mystrcat(arr1,arr2,2);
	printf("After connection:%s\n",arr1);

	return 0;
}

 

Topics: Linux