题目描述 :
思路分析:
1、 用“int *ans = (int *)malloc(sizeof(int)*numsSize);”构建指针变量ans;
2、*(ans+2)的值即为ans[2]的值,ans[2]=num[num2]];
3、创建指针函数buildArray,返回值为指针变量ans,为地址值;
关键代码展示:
(主要填写buildArray函数部分代码,主函数验证函数引用是否完成相应功能,具体待完善)
#include <stdio.h>
#include<stdlib.h>
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* buildArray(int* nums, int numsSize, int* returnSize){
// Please fill this blank
*returnSize=numsSize;
int *ans = (int *)malloc(sizeof(int)*numsSize);
for(int i=0;i<numsSize;i++){
ans[i]=nums[nums[i]];
}
return ans;
}
int main(void) {
int a[6]={0,2,1,5,3,4},*b,i,j;
b=buildArray(a,6,&i);
for(j=0;j<i-1;j++)
printf("%d,",b[i]);
printf("%d\n",b[j]);
return 0;
}
运行结果:
0,1,2,4,5,3
参考链接:
https://blog.csdn.net/weixin_44373236/article/details/121929990