自定义函数问题
/*用冒泡法对20个随机元素的数组排序*/#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void mppx(int a[])
{
int i, j, t, s;
s = sizeof(a) / sizeof(a);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
}
void main()
{
int b, i;
srand(time(NULL));
printf("排序之前\n");
for (i = 0; i < 20; i++)
{
b = rand() % 20;
}
for (i = 0; i < 20; i++)
{
printf("b[%d] = %-3d\n", i, b);
}
printf("排序之后\n");
mppx(b);//调用函数对=数组b进行排序
}
-----------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-cgfzor0y.cvw --stdout=Microsoft-MIEngine-Out-b3zzwf1o.v24 --stderr=Microsoft-MIEngine-Error-0irwvajd.iqa --pid=Microsoft-MIEngine-Pid-dutcx0ls.r0s --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
排序之前
b = 8
b = 15
b = 15
b = 13
b = 0
b = 13
b = 18
b = 14
b = 11
b = 1
b = 5
b = 14
b = 7
b = 14
b = 7
b = 11
b = 0
b = 2
b = 9
b = 19
排序之后
a = 8
E:\Administrator\Documents\My C>
=================================
为什么不能正确输出排序后的数组,到底哪里出问题了?????????????? /*用冒泡法对20个随机元素的数组排序*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int i, j, s;
int arr;
srand(time(NULL));
for (i = 0; i < 20; i++)
{
arr = rand() % 20;
}
s = sizeof(arr) / sizeof(arr);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
int t;
if (arr > arr)
{
t = arr;
arr = arr;
arr = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("arr[%d] = %d\n", i, arr);
}
}
============================================================
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-wh0sgtdm.v55 --stdout=Microsoft-MIEngine-Out-2ykotrdf.ufk --stderr=Microsoft-MIEngine-Error-lto0p5do.blo --pid=Microsoft-MIEngine-Pid-012pmyxn.z01 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
arr = 1
arr = 4
arr = 5
arr = 5
arr = 7
arr = 7
arr = 8
arr = 9
arr = 10
arr = 11
arr = 11
arr = 12
arr = 13
arr = 13
arr = 14
arr = 14
arr = 14
arr = 17
arr = 17
arr = 18
E:\Administrator\Documents\My C>
-----------------------------------------------------------------------------------------------------
为啥这样就可以,搞成自定义函数就不行 void mppx(int a[])
{
int i, j, t, s;
s = sizeof(a) / sizeof(a);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
}
当数组作为参数时传递的是指针而不是数组本身,也就是a的类型是int*,
所以 s=sizeof(a)/sizeof(a)即sizeof(int*)/sizeof(int)=2或1。
所以,在C语言中传递数组时,一般同时还会传递数组的大小:
void mppx(int a[],int s)
{
int i, j, t;
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
} 当将数组作为实参传递到一个函数中时,就是将数组的首地址作为实参,
函数的形参相当于一个指针,mppx函数里的sizeof(a)得到的是指针的大小为8,
而int大小为4,相除得2,所以你的程序只会排序前两个元素。
建议参考如下代码:
/*用冒泡法对20个随机元素的数组排序*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void mppx(int a[],int s)
{
int i, j, t;
// printf("s=%d",s);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
}
int main()
{
int b, i,s;
srand(time(NULL));
printf("排序之前\n");
for (i = 0; i < 20; i++)
{
b = rand() % 20;
}
for (i = 0; i < 20; i++)
{
printf("b[%d] = %-3d\n", i, b);
}
printf("排序之后\n");
s = sizeof(b) / sizeof(b);
mppx(b,s);//调用函数对=数组b进行排序
return 0;
}
页:
[1]