C language - advanced pointers (3)

Table of contents

1. Summary of the foreword

2. Simulation implementation of sorting function qsort

3. Analysis of written test questions on pointers and arrays


1. Summary of the foreword

Describes the calculation rules and usage of strlen and sizeof for various arrays and pointers. There is also a simulation implementation of the qsort function (can sort variables of any type)

2. Simulation implementation of sorting function qsort

Goal: Use the idea of ​​​​bubble sorting to simulate and implement a sorting function that can sort any type of data.

Let’s do two small tests first:

Test one:

Bubble sort is already a regular customer. As long as you understand the number of times for each comparison (worst case scenario), you can write it out using a double-layer for loop.

 As soon as the test is over, the conclusion is: only integer arrays can be sorted.

Test 2:

When we enter actual parameters into the bubble_sort function , we will find that the variable type that accepts the actual parameters can only be int, which does not match the structure.

Next we will modify the bubble_sort function again.

When you get ashore, behead the person you like first, and you need to modify the shape and form of the letter. We can find that the actual parameters of the two test transmissions are addresses, so we choose to use the Tiger Balm void* pointer (which can receive any type of address) to receive.

In addition to knowing the starting address and number of elements, you also need to know the size of an element, so that you can know where the next element is stored. So we introduced a new variable size_t size.

The following is an analysis of the internal sorting:

First of all, the number of passes is the same whether it is integer sorting or structure sorting, so you need to compare them slowly. The only thing that needs to change is this piece of code.

For different types of comparisons, the comparison methods of two elements are also different. You cannot use > to compare structures.

We might as well provide a function that specifically compares the sizes of two elements. If the previous element is >0, then let cmp return a number greater than 0. If they are equal, return 0. If the latter element is greater, return a number less than 0.

So how do we call this cmp function in the function? We can add a formal parameter. This formal parameter is a function pointer that stores the address of the cmp function (so that we can point to the address and call it), and this The two parameters e1 and e2 pointed to by the function pointer are the two elements we need to compare (here, pointers are used to receive them). Finally, the return type of the function pointer is int (returned because the size of the two elements is divided into different values).

Because we need to perform a subtraction operation, we perform forced type conversion on e1 and e2 before dereferencing them.

Next we have to think about how to pass the addresses of arr[j] and arr[j+1] to the cmp function:

From the illustration, we can know that base points to the address of the first element. Can we base+1 it to point to the next element? The answer is no. Although void* is a snake oil that can receive any type of address, it also has disadvantages-that is, it cannot +- integer to change the pointer pointing.

It is not appropriate to force it to int*, so we can simply force it to char*. When we need to point to the next element and know the element size, we can jump to the point by adding it directly. At the beginning, we had a ratio of 9 and 8. How can we achieve a ratio of 9 and 7 later?

Since the essence is the comparison of adjacent elements, we connect it with j and use j*size to compare two adjacent elements in real time.

We also need to make corresponding changes when exchanging two elements. Since the type of the element is not clear, we use a byte-for-byte exchange.

Some people may be confused, why not directly create a third variable exchange here? If we create a third variable, we won't be able to create it if we don't know the variable type.

We can exchange the bytes of two elements one by one through temporarily created bytes (char type)

At this point, the transformation of this general type of sorting function is completed. Let's first test the integer array test1.

Overall structure diagram:

Next we test the age in the structure:

Create another function that compares structures

When we compare by age:

If you want to compare by name:

Then create another function, but string comparison requires strcmp for comparison.

Overall structure diagram:

When we want to change ascending order to descending order, let's take a look at what should be changed.

3. Analysis of written test questions on pointers and arrays

Appetizer:

Integer type:

character:

A special explanation is needed here for &arr. Although the strlen receiving type is received with const char* str, the array pointer char(*)[6] will undergo type conversion, but essentially the entire array address and the first element address are the same at the beginning. , so strlen will still look for \0 starting from the first address.

Here’s another set:

This is not about saving abcdef into p. It is more accurate to save the address of a into p so that p can find the string abcdef\0.

Guess you like

Origin blog.csdn.net/fax_player/article/details/132889085