关于C语言的,指向指针的指针,需要专业知识,懂的进。

来源:百度知道 编辑:UC知道 时间:2024/06/08 02:36:30
用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。

整数和n在主函数中输入,最后在主函数中输出。

大家帮忙看看是哪儿出问题了,为什么结果不对?

#include <stdio.h>
void Sort(int **p, int times)
{
int *temp, i=0, j=0;
for (i=0; i<times-1; i++)
{
for (j=i+1; j<times; j++)
{
if (**(p+i) > **(p+j))
{
temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
}
}
}
}
void main()
{
int *str[20], i=0, times=0;
printf("input times to opt:\n");
scanf("%d", ×);
printf("Please input the figures:\n");
for (i=0; i<20; i++)
{
str[i]=(int*)malloc(sizeof(int));
}
for (i=0 ; i<times; i++)
{
scanf("%d", str+i);
}
printf("\nthe re

如楼上说的三、四处确实有问题。
scanf("%d", str+i);应该改为scanf("%d", *(str+i));
scanf的第二个参数是指针类型,而str+i是指针的指针类型。*(str+i)才是指针类型。
printf("%-6d", str[i]);应该改为printf("%-6d", *(str[i]));
因为str[i]是一个指针,所以应该用*取值。
楼主你用什么编译器啊?我用gcc立刻发现这两个警告了。

#include <stdio.h>
#include <malloc.h>//修正一处
#include <stdlib.h>//修正二处
void Sort(int **p, int times)
{
int *temp, i=0, j=0;
for (i=0; i<times-1; i++)
{
for (j=i+1; j<times; j++)
{
if (**(p+i) > **(p+j))
{
temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
}
}
}
}
void main()
{
int *str[20], i=0, times=0, n=0;
printf("input times to opt:\n");
scanf("%d", ×);
printf("Please input the figures: