我希望按照第一个数组对第二个数组进行排序。例如
first = {1,8,7,2,4}
second = {9,7,2,10,3}
Run Code Online (Sandbox Code Playgroud)
我希望第一个保持不变,第二个以与第一个相同的相对顺序进行排序。即,最低值在索引0处,第二最低值在索引3处,第三最低值在索引4处,依此类推,等等
second = {2,10,9,3,7}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下代码
#include <stdio.h>
typedef struct
{
int num;
int pos;
}ArrType;
ArrType arrA[5] = {{1,0},{8,1},{7,2},{2,3},{4,4}};
ArrType arrB[5] = {{9,0},{7,1},{2,2},{10,3},{3,4}};;
int cmparr(const void *a, const void *b)
{
ArrType *tmpa, *tmpb;
tmpa = (ArrType*) a;
tmpb = (ArrType*) b;
return(arrA[tmpa->pos].num - arrA[tmpb->pos].num);
}
int main(void)
{
int i;
qsort(arrB,5, sizeof(ArrType), cmparr);
for (i=0; i<5; i++)
{
printf ("%d ",arrB[i].num);
}
return (0);
}
Run Code Online (Sandbox Code Playgroud)
实际输出为
9 10 3 2 7
我对另一种数据结构持开放态度,但 …
每次运行代码时,我都会得到不同的输出值。当堆和堆栈地址固定时,为什么 malloc 返回不同的地址?我希望它从堆顶部开始分配并每次返回一个固定地址。对于堆栈也是如此。
#include <stdio.h>
#include <stdlib.h>
int main(){
int *ptr = malloc(128);
int a;
printf("%p %p\n", ptr, &a);
return 1;
}
Run Code Online (Sandbox Code Playgroud) 我知道文本段是只读段,尝试写入它会导致“总线错误”。我很好奇这个段是如何变成只读的。
由于物理内存不是只读的,因此必须在分页期间完成此操作。
内存的每个页面是否都有一个位用于为文本段设置的只读页面?