确实挺奇怪的,我这边一切正常
$ cat main.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<string.h>
void cop(int (*a)[],int b);
void ran(int (*a)[],int b);
void ran(int (*a)[],int b)
{
srand(time(0));
for(int i=0;i<b;i++)
{
*((*a)+i)=rand()%150;
//sleep(1);
}
for(int i=0;i<b;i++)
{
printf("%d ",(*a)[i]);
}
putchar('\n');
}
void cop(int (*a)[],int b)
{
int temp=0;
for(int i=0;i<b-1;i++)
{
for(int j=i+1;j<b;j++)
{
if((*a)[i]>(*a)[j])
{
temp=(*a)[j];
(*a)[j]=(*a)[i];
(*a)[i]=temp;
}
}
}
for(int i=0;i<b;i++)
{
printf("%d ",(*a)[i]);
}
putchar('\n');
}
int main()
{
int b=0;
printf("请输入数组数字个数:");
scanf("%d",&b);
int a[b];
int (*p)[b]=&a;
ran(p,b);
cop(p,b);
return 0;
}
$ gcc-debug -o main main.c
$ ./main
请输入数组数字个数:6
10 3 97 60 125 104
3 10 60 97 104 125
$ ./main
请输入数组数字个数:7
37 41 36 126 0 79 92
0 36 37 41 79 92 126
$ ./main
请输入数组数字个数:8
82 83 123 49 25 18 48 20
18 20 25 48 49 82 83 123
$ ./main
请输入数组数字个数:20
29 102 51 29 57 133 63 120 15 16 135 138 109 117 23 132 72 138 140 121
15 16 23 29 29 51 57 63 72 102 109 117 120 121 132 133 135 138 138 140
$ gcc --version
gcc (GCC) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ uname -a
Linux Suhuajun-Archlinux 5.18.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 02 Jul 2022 21:03:06 +0000 x86_64 GNU/Linux
$
|