对动态一维数组排序然后值可以影响到主函数的
# include<stdio.h># include<cstdlib>
# include<malloc.h>
void f(int ** q, int len)
{
int i, j;
int t;
for (i = 0; i < len; ++i)
{
scanf_s("%d", &q[i]);
}
for (i = 0; i < len; ++i)
{
printf("%d\n", q[i]);
}
printf("\n\n");
printf("%d", q);
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i; ++j)
{
if (q[j] > q[j + 1])
{
t = q[j];
q[j] = q[j + 1];
q[j + 1] = t;
}
}
}
for (i = 0; i < len; ++i)
printf("%d\n", q[j]);
}
int main(void)
{
int len;
int * pArr;
scanf_s("%d", &len);
pArr = (int *)malloc(sizeof(int)* len);
f(&pArr, len);
system("pause");
return 0;
}
对动态一维数组排序然后值可以影响到主函数的 这个程序行不通,求大神帮忙过目; 下面那个程序也是同类型的但修改子函数的不影响主函数的下面程序可行但不是想要的结果, 求结果子函数排序后的数组内容也同时更改了主函数的内容; 这问题搞了两天了头大 还求大神把指针与数组详细化点 普通的指针可以应付但对于数组还是头疼
# include<stdio.h>
# include<cstdlib>
# include<malloc.h>
void f1(int * j, int len)
{
int i;
printf("排序后\n");
for (i = 0; i < len; ++i)
{
printf("%d\n", j[i]);
}
}
void f(int * q, int len)
{
int i, j;
int t;
for (i = 0; i < len; ++i)
{
scanf_s("%d", &q[i]);
}
for (i = 0; i < len; ++i)
{
printf("%d\n", q[i]);
}
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i; ++j)
{
if (q[j] < q[j + 1])
{
t = q[j];
q[j] = q[j + 1];
q[j + 1] = t;
}
}
}
f1(q, len);
}
int main(void)
{
int * pArr;
int len;
int i;
printf("请输入数组长度");
scanf_s("%d", &len);
pArr = (int *)malloc(sizeof(int)* len);
f(pArr, len);
system("pause");
return 0;
}