C language -- 2. Notes on pointer array and array pointer and function pointer

Posted by Iceman512 on Sat, 26 Feb 2022 01:35:05 +0100

1. Pointer array and array pointer concept
(1) Pointer array: it is an array of pointers, and the elements in the array are arrays.
(2) Array pointer: it is a pointer to an array, a pointer to an array.
2. Expression

(1)int *p[5] 
//  Pointer array, combined with square brackets of p, is an array, so it is a pointer array 
// This array has five elements, each of which is a pointer,
// The element type pointed to by the pointer is of type int.

(2)  int (*p)[5]
// Array pointer, p is first combined with the asterisk, which is a pointer, so it is an array pointer
// This pointer points to an array of five elements (each of type int)

(3) int *(p[5])
// Parentheses are meaningless. They are the same as the first one

int main(void)
{
	int *p;
	int a[5];
	
	p = a;//The types match
	// p = &a;   Type mismatch, P is int * type,
	// &A is of type int (*) [5], that is, an array pointer
int (*p1)[5];
p1 = &a;//This is OK

return 0;
}

3. Function pointer and typedef
3.1 essence of function pointer: it is still a pointer variable, which accounts for 4 bytes, which is consistent with the ordinary pointer, but what the function pointer points to is the address of the first sentence code of a function (expressed by function name in C language).

3.2 writing and analysis method of function pointer

void func1 (void)
{
	printf("test for function pointer.\n");
	
}
 
int main (void)
{
	voiod (*pFunc)(void);
	pFunc = func1;// On the left is a function pointer variable, and on the right is a function name.
	//The meaning is consistent with pffunc = & func1, which is different from the array name of the array
	
	pFunc();// Use the dereference of the function pointer to call the function [not understood here]

return 0;
}

This is a blog about pointer function and function pointer!!! Please watch more

4. typedef keyword
4.1 typedef keyword is used to define. Redefining is the type of data, not variables.

5. Function pointer practice

// Function pointer practice
#include<stdio.h>

int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int dev(int a,int b);

// A function pointer pFunc is defined, which points to int. the transfer parameter of int type and the function of return value of int type
typedef int (*pFunc)(int,int);

int main (void)
{
	pFunc p1 = NULL;
	char c = 0;
	int a = 0,b = 0,result = 0;
	printf("Please enter the operation type:+ — * /\n");
	scanf("%c",&c);
	switch(c)
	{
		case '+':p1 = add;break;
		case '-':p1 = sub;break;
		case '*':p1 = mul;break;
		case '/':p1 = dev;break;
		default:p1 = NULL;break;
	}
	printf("Please enter two integers to operate on:\n");
	scanf("%d  %d",&a,&b);
	result = p1(a,b);
	printf("%d %c %d = %d.\n",a,c,b,result);
	return 0;
}
int add(int a,int b)
{ 
	return a + b;
}
int sub(int a,int b)
{
	return a - b;
}
int mul(int a,int b)
{
	return a * b;
}
int dev(int a,int b)
{
	return a / b;
}

6. On typedef
6.1 two types of C language: built-in type and user-defined type
Built in type ADT, custom type UDT

6.2 typedef defines types, not variables (i.e. redefinition)
(1) Type is a data template, and variable is a real data. Types do not occupy memory, while variables occupy memory.
(2) In object-oriented language, type is class and variable is object.

6.3 the difference between typedef and #define

typedef char *pChar;
#define pChar char *

6.4 typedef and structure
(1) Structure type is defined first, and then structure variable is defined

// First kind
struct student      // Corresponding structure pointer: struct student *p1;
{
	char name[4];
	int age;
};
// Second
typedef struct student  // Redefine the structure type as student_t
{
	char name[4];
	int age;
}student_t;
 // Corresponding structure pointer: student_t *p2;

// The third way is to define structure pointers
typedef struct teacher
{
	char name[4];
	int age;
}teeacher, *pTeacher;// Two types are redefined, the first is the structure, the second is the structure pointer type, and its variable is pTeach


int main(void)
{
struct student S1;// struct student is the type and S1 is the variable
student.name = cao;

student_t S2;
student.name = jiao;

return 0;
}

(2)typedef and const

typedef int * PINT;
const PINT p2;   // Equivalent to int *const p2;

typedef int * PINT;
PINT const p2;   // Equivalent to int *const p2;

If you just want it const int *p;can only typedef const int *CPINT;CPINT p1;

7. Double pointer
7.1 difference between double pointer and ordinary single pointer
Double pointer and double pointer are essentially pointer variables, and both single pointer variables and double pointer variables occupy 4 bytes of memory space.

Char * * type means that the variable pointed to by the pointer is char * type (double pointer refers to a single pointer), and char * type means that the variable pointed to is char type.

int main(void)
{
	int *p1[5];
	int *p2;
	int **p3;
	// p2 = p1;
	// This code is wrong
	p3 = p1;
return 0;
}

*Code understanding: p1 is the pointer array name, which is essentially the array name. The right value of the array name represents the first address of the first element. The element of the array is of type int, so the right value of p1 represents the address of an int type variable, so p1 is the pointer of a pointer to an int type variable, so p1 is a double pointer. (I understand the pointer array in this way: because the element of the pointer array is of type int (that is, a double pointer), if you use the pointer array name to access the first element of the pointer array, and the first element is a double pointer, then the pointer array name is a double pointer.).


Summary:
1. The double pointer is rarely used, which is generally entangled with the pointer array.
2. The address of the single pointer matches the double pointer. (the above code: P3 = & P2, you can understand the & symbol)

8. Two dimensional array
For example, two-dimensional array a[2][5], I understand it this way: the two elements in one-dimensional array a[2] are also one-dimensional array a[5];

8.1 subscript access and pointer access of array
(1) One dimensional array:
Take int b[10] as an example, int p = b; b[0] is equal to (p+0); b[5] is equivalent to * (p+9); b[i] is equivalent to * (b+i).
(2) 2D array:
Take int a[3][5] as an example, int *p = a; a[1][2] is equivalent to ((p+1) + 2).
a[i][j] is equivalent to ((p+i) + j).
My understanding is: the inner layer (p+i) represents the row of the matrix (the first dimension), and the outer pointer determines the column of the matrix (the second dimension).

Topics: C