C language: Advanced pointer

Posted by devangel on Sun, 30 Jan 2022 04:03:10 +0100

  1. Character pointer
  2. Array pointer
  3. Pointer array
  4. Array parameter passing and pointer parameter passing
  5. Function pointer
  6. Function pointer array
  7. Pointer to array of function pointers
  8. Callback function
  9. Pointer and array analysis of interview questions

 

  • Character pointer

    • One of the pointer types is character pointer char*
    • General use
char* p="abcde"; //Constant string, cannot be changed
//The starting address of the stored string p is

The exact way of writing is plus const modification
const char* p="abcde";

Face to face questions:

int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcdef";
	const char* p1 = "abcdef";
	const char* p2 = "abcdef";   //P1 and P2 have the same address
	if (arr1 == arr2)
	{
		printf("hehe\n");
	}
	else
	{
		printf("haha\n");
	}
	if (p1 == p2)
	{
		printf("hehe\n");
	}
	else
	{
		printf("haha\n");
	}
	return 0;
}
  • Array pointer

    • Is a pointer that can point to the array. It is used to store the address of the array. Int (* P) [n] = & arr;    *p==arr
    • 	char* parr[5];
      	char* (*pa)[5] = &parr;

    • Basic usage;

      • #include <stdio.h>
        
        //Parameters are arrays
        void print1(int arr[3][5],int x,int y)
        {
        	int i = 0;
        	int j = 0;
        	for (i = 0; i < x; i++)
        	{
        		for (j = 0; j < y; j++)
        		{
        			printf("%d ", arr[i][j]);
        		}
        		printf("\n");
        	}
        }
        //Parameters are pointers
        void print2(int(*p)[5], int x, int y)
        {
        	int i = 0;
        	for (i = 0; i < x; i++)
        	{
        		int j = 0;
        		for (j = 0; j < y; j++)
        		{
        			//arr[i] == *(arr+i) == *(p+i) == p[i]
        			//*(p+i) == p[i]     *(p[i]+j) == p[i][j]
        			printf("%d ", *(*(p + i)+j));
        		}
        		printf("\n");
        	}
        }
        int main()
        {
        	int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };
        	print1(arr, 3, 5);//The address of the first row of the first element address of the arr array name
        	print2(arr, 3, 5);
        	return 0;
        }

  • Pointer array

    • Is an array used to store pointers
  • #include <stdio.h>
    
    int main()
    {
    	int i = 0;
    	int j = 0;
    	int arr1[] = { 1,2,3,4,5 };
    	int arr2[] = { 2,3,4,5,6 };
    	int arr3[] = { 3,4,5,6,7 };
    	int* arr[] = { arr1,arr2,arr3 };
    	for (i = 0; i < 3; i++)
    	{
    		for (j = 0; j < 5; j++)
    		{
    			printf("%d\t", *(arr[i] + j));
    		}
    		printf("\n");
    	}
    	return 0;
    }

     

  • Array parameter passing and pointer parameter passing

    • One dimensional array parameters can be passed by array or pointer, and the array size can be omitted
      • void test( int arr[ ] )   void test( int* arr )
    • Two dimensional array parameters can be transferred by two-dimensional array or array pointer. The rows of the array can be omitted and the columns cannot be omitted
      • void test( int arr[ ][ N ] )    void test( int (*arr)[ 5 ])
    • The primary pointer is used to transfer parameters
      • int a = 10;
        int* p = &a;
        test(p);
        
        
        
        void test(int* p)
        {
            
        }

         

    • The second level pointer or the first level pointer array is used for parameter transfer
    • int n = 10;
      int* p = &n;
      int** pp = &p;
      test(pp);
      test(&p);
      
      int* arr[10];
      test(arr);
      
      
      
      void test(int** ptr)
      {
          
      }

       

  • Function pointer

    • Is a pointer to a function, where the address of the function is stored / / & both the function name and the function name are the address of the function
    • Definition: return type (* p) (parameter type) p is the function pointer
    • #include <stdio.h>
      
      int add(int x,int y)
      {
      	int z = x + y;
      	return z;
      }
      int main()
      {
      	int (*pa)(int, int) = add;
          //Both are OK
      	printf("%d\n", (pa)(2, 3));
      	printf("%d\n", (*pa)(2, 3));//*It doesn't work
      	return 0;
      }

       

    • //Code 1
      //void(*) () 0 converts the mandatory type of 0 to the type of void(*) () function pointer. 0 is a function address
      //Call the function at address 0
      (* (void (*)())0 )();
      
      
      
      //Code 2
      //signal is a function declaration
      //The return type of signal is a function pointer
      void (*  signal(int,void(*)(int))  )(int);
      
      typedef void(* pfun_t)(int);
      pfun_t signal(int,pfun_t);
      
      
      typedef unsigned int uint;
      

      C traps and defects

  • Function pointer array

    • An array of function pointers
      • Purpose: transfer table

      • #include <stdio.h>
        
        int add(int x, int y)
        {
        	return x + y;
        }
        
        int sub(int x, int y)
        {
        	return x - y;
        }
        int mul(int x, int y)
        {
        	return x * y;
        }
        
        int div(int x, int y)
        {
        	return x / y;
        }
        
        void menu()
        {
        	printf("********************\n");
        	printf("***1.add    2.sub***\n");
        	printf("***3.mul    4.div***\n");
        	printf("****** 0.exit ******\n");
        }
        
        int main()
        {
        	int input = 0;
        	int x = 0;
        	int y = 0;
            //pfarr is a function pointer array -- transfer table
        	int (*pfarr[5])(int, int) = {0, add,sub,mul,div };
        	do
        	{
        		menu();
        		printf("Please select mode:\n");
        		scanf_s("%d", &input);
        		if (input >= 1 && input <= 4)
        		{
        			printf("Please enter two operands:\n");
        			scanf_s("%d%d", &x, &y);
        			int ret = pfarr[input](x, y);
        			printf("The result is:%d\n", ret);
        		}
        		else if (input == 0)
        		{
        			printf("sign out.......\n");
        		}
        		else
        		{
        			printf("Input error, please reselect:\n");
        		}
        	} while (input);
        }

         

  • Pointer to array of function pointers

    • The pointer to the array of function pointers is a pointer, the pointer points to an array, and the array elements are function pointers
    • int arr[10] = { 0 };
      	//Pointer array
      	int (*p)[10] = &arr;
      	//Array of function pointers
      	int(*parr[10])(int, int);
      	//pparr is an array pointer. The array pointed to by the pointer has 10 elements. The element type is function pointer
      	int(*(*pparr)[10])(int, int) = &parr;

       

  • Callback function

    • A callback function is a function called through a function pointer. If you pass the pointer (address) of the function as a parameter to another function, when the pointer is used to call the function it points to, we say it is a callback function. The callback function is not called directly by the function, but by another party when a specific event or condition occurs, Used to respond to this event condition
    • #include <stdio.h>
      
      //Callback function
      void print(char* str)
      {
      	printf("hehe:%s\n", str);
      }
      
      void test(void (*p)(char*))
      {
      	printf("test\n");
      	p("bit");
      }
      
      int main()
      {
      	test(print);
      
      	return 0;
      }

      Pointer of void * type cannot be dereferenced

    •  

  • Pointer and array analysis of interview questions

Topics: C