*(int *)b - *(int *)a;
时间: 2024-06-17 14:07:27 浏览: 20
这是一个C语言的表达式,表示用指针a所指向的整数减去指针b所指向的整数。其中(int *)表示将指针转换为指向整型的指针。这个表达式可以用来实现一个降序排序的函数,比如使用qsort函数。下面是一个例子:[^1]
```C
#include <stdio.h>
#include <stdlib.h>
// 比较函数,用于qsort降序排序
int cmpfunc (const void * a, const void * b) {
return ( *(int*)b - *(int*)a );
}
int main () {
int arr[] = {10, 5, 6, 8, 4, 2, 3, 11, 9, 1, 7};
int n = sizeof(arr)/sizeof(int);
// 使用qsort函数降序排序
qsort(arr, n, sizeof(int), cmpfunc);
// 输出排序后结果
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
运行结果为:
```
11 10 9 8 7 6 5 4 3 2 1
```
相关问题
return *(int *)a - *(int *)b;
`return *(int *)a - *(int *)b;` 这是一个C/C++语言中的表达式,它涉及到指针运算。这里解释一下每一部分:
1. `*(int *)a`: 这表示取`a`指向的地址中的值,因为前面有一个星号(*),意味着解引用操作,而`int *`表示`a`是一个指向整数(int)的指针。
2. `*(int *)b`: 类似地,这个表达式取`b`指向的地址中的整数值。
3. `-`: 这个操作符表示减法,即将`a`指向的整数值从`b`指向的整数值中减去。
所以整个表达式的含义是返回两个整数相减的结果。这个表达式通常用于比较或计算两个存储在内存不同位置的整数值。
*(int*)a-*(int*)b
This is a C expression for subtracting two integers.
- `a` and `b` are pointers to memory locations where integers are stored.
- `(int*)a` and `(int*)b` cast the pointers to type `int*`, indicating that the memory locations should be treated as integers.
- `*(int*)a` and `*(int*)b` dereference the pointers to retrieve the integer values stored in those memory locations.
- The two integers are then subtracted using the `-` operator.
Overall, the expression computes the difference between the integer values stored at memory locations pointed to by `a` and `b`.