for(int j=i+1;j
for(int j=i+1;j<sizeof(p_array)/sizeof(short)-1;j++)//怎么进不到循环判断里呢?
#include <time.h>
#include <iostream>
using namespace std;
struct res{
int a;
};
short _array[5] = {4,1,3,2,5};
res fun_res(short* p_array)
{
int temp=0;
for(int i=0;i<sizeof(p_array)/sizeof(short);i++)
for(int j=i+1;j<sizeof(p_array)/sizeof(short)-1;j++)//怎么进不到循环判断里呢?
{
//static
if(p_array[i] > p_array[j])
{
if(p_array[i] > temp)
temp = p_array[i];
}
}
return (res.a = temp);//为什么语法错误?
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
clock_t t1 = clock();
//database_conrast(access_path,db_path);
printf("%d\n",fun_res(_array));
clock_t t2 = clock();
std::cout<<"time: "<<t2-t1<<std::endl;
}
------解决方案--------------------
sizeof(p_array) = 4 //p_array是指针
sizeof(short) = 2
for(int j=i+1;j<sizeof(p_array)/sizeof(short)-1;j++)
j = i+1 = 1
sizeof(p_array)/sizeof(short)-1 = 4/2 - 1 = 1
------解决方案--------------------
说的对,因为p_array是指针,sizeof(p_array)等于4.
所以你的接口应该这么写
res fun_res(short* p_array, int size)
{
... ...
}
调用时
fun_res(_array, sizeof(_array)/sizeof(short)-1);
#include <time.h>
#include <iostream>
using namespace std;
struct res{
int a;
};
short _array[5] = {4,1,3,2,5};
res fun_res(short* p_array)
{
int temp=0;
for(int i=0;i<sizeof(p_array)/sizeof(short);i++)
for(int j=i+1;j<sizeof(p_array)/sizeof(short)-1;j++)//怎么进不到循环判断里呢?
{
//static
if(p_array[i] > p_array[j])
{
if(p_array[i] > temp)
temp = p_array[i];
}
}
return (res.a = temp);//为什么语法错误?
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
clock_t t1 = clock();
//database_conrast(access_path,db_path);
printf("%d\n",fun_res(_array));
clock_t t2 = clock();
std::cout<<"time: "<<t2-t1<<std::endl;
}
------解决方案--------------------
sizeof(p_array) = 4 //p_array是指针
sizeof(short) = 2
for(int j=i+1;j<sizeof(p_array)/sizeof(short)-1;j++)
j = i+1 = 1
sizeof(p_array)/sizeof(short)-1 = 4/2 - 1 = 1
------解决方案--------------------
说的对,因为p_array是指针,sizeof(p_array)等于4.
所以你的接口应该这么写
res fun_res(short* p_array, int size)
{
... ...
}
调用时
fun_res(_array, sizeof(_array)/sizeof(short)-1);