[embedded C language series] Pointer

Pointer

Initializing pointer variables

A pointer is a variable whose value is a memory address.
Just as the value of a char type variable is a character, the value of an int type variable is an integer, and the value of a pointer variable is an address.

To create a pointer variable, first declare the type of the pointer variable.

int *pi; // pi is a pointer to a variable of type int
char *str; // str is a pointer to a variable of type char
float *pf, *pg; // PF and PG are pointers to variables of type float only
// Initialize while defining
int a = 5; int *p = &a;
// Define before initialize
int a = 5; 
int *p;
p=&a;
// Initializes the pointer to NULL
int *p=NULL; 
int *q=0;

The type specifier indicates the type of object pointed to by the pointer, and the dereference symbol * indicates that the declared variable is a pointer. int *pi declaration means that pi is a pointer and * pi is an int type, as shown in figure 5.3.4.

Access the storage space pointed to by the pointer

  • The address operator & is provided in C language to represent the address of variables. Its general form is:
 int a = 5;
 int *p = &a; 
 int b = 10; 
 p = &b; // Modify pointer pointing
  • &Variable name;

  • C language provides * to define pointer variables and access the memory storage space pointed to by pointer variables. When defining variables, * is a type specifier, indicating that the defined variable is a pointer variable

int *p=NULL; // Define pointer variables
  • When a variable is not defined, * is an operator representing the storage space pointed to by the access pointer
int a = 5; 
int *p = &a; 
printf("a = %d", *p); // Access pointer variable

Pointers and arrays

As mentioned earlier, the address operator & can be used to obtain the address of the variable. In the array, the address operator can also be used to obtain the address of any member in the array, for ex amp le:

int week[7] = {1, 2, 3, 4, 5, 6, 7};
int *pw;
pw = &week[2];
printf("week is: %d", *pw);

The output is: week is 3

Pointers and functions

Formal parameter

The simplest use of pointers in functions is as formal parameters of functions, such as:

int sum(int *pdata)
{
	int i = 0;
	int temp = 0;
	for(i=0;i<10;i++) 
	{
		temp = temp + (*pdata);
		pdata++;
	}
	return temp;
}
  1. The pointer pdata exists as a formal parameter of a function and points to an address where int type variables are stored;
  2. Pointer pdata + +. After the statement is executed, the address pointed to by pdata will not increase by 1, but the size occupied by int type. The initial value added to pdata is 0, and int type accounts for 2 bytes, then pdata + +; After the statement is executed, the value of pdata becomes 2 instead of 1, and the value of * pdata is the value of address 2, not the value of address 1;
  3. There is a danger in this function, that is, the function implements the sum of 10 int variables starting from the address initially pointed to by pdata. If we use this method:
int data[5] = {1, 2, 3, -1, -2};
int x = sum(data);

You can see that the array name of array data, that is, the first address of the array, is input into the function sum as a parameter, and the size of the array is only 5 ints, but the function sum calculates the sum of 10 numbers. Therefore, address overflow will occur, the correct result will not be obtained, and even the program will fly. To avoid this problem, the usual solution is to add a quantity parameter:

int sum(int *pdata, int length)
{
	int i = 0;
	int temp = 0;
	for(i=0;i<length;i++) 
	{
		temp = temp + (*pdata);
		pdata++; }
		return temp;
	}
x = sum(data, 5);

Or give the pointer range:

int sum(int *pStart, int *pEnd)
{
	int i = 0;
	int temp = 0;
	int length = (pEnd - pStart)/2; // Suppose an int takes up 2 bytes
	
	for(i=0;i<length;i++) 
	{
		temp = temp + (*pdata);
		pdata++; }
		return temp;
	}
	x = sum(data, &data[4]);

Function pointer

typedef void (*pFunction)(void);

*Indicates that pFunction is a pointer variable, and the preceding void indicates that the pointer variable returns a void
The value of type. The void in the last bracket indicates that the formal parameter of the function pointer is of type void.

Function pointer calling function:

int max(int a, int b)
{
	return ((a>b)?a:b);
}

int main(void) 
{
	int (*pfun)(int, int);
	int a=-1, b=2, c=0;
	
	pfun = max;
	c=pfun(a, b);
	printf("max: %d", c);
	return 0; 
	///The output is 2
}

Pointer and hardware address

The connection between pointer and hardware address is startled in the example of volatile usage chapter. It is not introduced in detail. It is described in detail below. For example, in STM32F103ZET6, the base address of the internal SRAM is 0x20000000. If we want to write data to the first 256 bytes of this space, we can point to the base address with a pointer and start writing:

volatile unsigned char *pData = (volatile unsigned char *)(0x20000000);

int main(void)
{
int i = 0;
for(i=0; i<256; i++) 
{
	pData[i] = i+10; 
}
return 0; 
}

Basic principles of pointer application:

  • First, you must specify the type of pointer;
  • If it is an ordinary pointer variable, non function parameter or function pointer, you must specify the address of the pointer variable to avoid becoming a "wild pointer";

Keywords: C Back-end

Added by Nameless12 on Fri, 21 Jan 2022 11:41:14 +0200