求一数组程序解释(有个函数看不懂)

discory 2013-01-11 02:38:39
#include <iostream>

using namespace std;

void someFunction( int [], int );

int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 32, 27, 64, 18 ,95, 14, 90, 70, 60, 37 };

cout << "The values in the array are: " << endl;
someFunction( a, arraySize );
cout << endl;
system("pause");
return 0;
}

void someFuntion( int b[], int size )
{
if ( size > 0 )
{
someFunction( &b[1], size - 1 ); //这里看不懂, &b[1] ?
cout << b[0] << " ";
}
}


这个应该是递归调用吧,但是有个参数没搞明白,还有这个程序在vs2012上运行通不过,程序是看书上写的,
错误提示:
错误 1 error LNK2019: 无法解析的外部符号 "void __cdecl someFunction(int * const,int)" (?someFunction@@YAXQAHH@Z),该符号在函数 "void __cdecl someFuntion(int * const,int)" (?someFuntion@@YAXQAHH@Z) 中被引用 D:\ex\8\源.obj 8
错误 2 error LNK1120: 1 个无法解析的外部命令 D:\ex\8\Debug\8.exe 8
...全文
127 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
discory 2013-01-11
  • 打赏
  • 举报
回复
哦,感谢vipjeffreylee, 是我写漏了,现在正确了,2位大虾的解释也搞明白了,谢谢
vipjeffreylee 2013-01-11
  • 打赏
  • 举报
回复
#include <iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;

void someFunction(int[], int );

int main()
{
    const int arraySize = 10;
    int a[arraySize] = { 32, 27, 64, 18 ,95, 14, 90, 70, 60, 37 };

    cout << "The values in the array are: " << endl;
    someFunction(a,arraySize );
    cout << endl;
    system("pause");
    return 0;
}

void someFunction(int *p,int size )
{
    if ( size > 0 )
    {
        someFunction(p+1, size - 1 );   //这样你应该好理解吧,跟你的程序完全等价
        cout << *p << " ";
    }
}
discory 2013-01-11
  • 打赏
  • 举报
回复
第一个参数是指针?我理解的是把someFunction(a, arraySize)的a数组当参数通过引用调用 传到someFunction(int b[], size),这都正确的,你说的函数的一个参数要求是指针是指?还没明白
vipjeffreylee 2013-01-11
  • 打赏
  • 举报
回复
代码有出错误,所以导致你编译出错,someFuntion 定义函数时错了,应该是somefunction 漏掉一个c,修改如下
#include <iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;

void someFunction(int[], int );

int main()
{
    const int arraySize = 10;
    int a[arraySize] = { 32, 27, 64, 18 ,95, 14, 90, 70, 60, 37 };

    cout << "The values in the array are: " << endl;
    someFunction(a,arraySize );
    cout << endl;
    system("pause");
    return 0;
}

void someFunction(int b[],int size )
{
    if ( size > 0 )
    {
        someFunction(&b[1], size - 1 );   // &b[1] 每一次递归,向后顺延数组一个元素,也就是说,第一次是
                                           //传入的是a数组,第二次传入的是a+1,依次类推
        cout << b[0] << " ";
    }
}
whizer 2013-01-11
  • 打赏
  • 举报
回复
someFunction( &b[1], size - 1 ); //这里看不懂, &b[1] ? &b[1]取数组b中第一个元素的地址,你的函数的第一个参数要求是指针.

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧