Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
我在C ++程序上遇到麻烦。它一直给我一个运行时错误#2-S。尽管它运行良好。它构建良好。 Main应该调用另外两个函数,一个函数将整数放入数组中,另一个函数将数字在数组中的位置取反,本质上创建了一个“反向”数组。

#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;

void storeArray(int[], int);
void flipArray(int[], int);

int main()
{
const int arraysize = 10;
int foo[arraysize]; // everyone seems to use foo so I thought I'd try it

storeArray(&foo[arraysize], arraysize);

cout << endl;

flipArray(&foo[arraysize], arraysize);

return 0;
}

void storeArray(int foo[], int arraysize)
{
int counter;
counter = 0;

while (counter < arraysize)
{

    cout << "Enter number " << (counter+1) << " : ";
    cin >> foo[counter];        counter++;

}

return;
 }

void flipArray(int foo[], int arraysize)
{

int counter2;
int counter3;

counter2 = 0;
counter3 = 9;

for (counter2; counter2 <= 9; counter2++)
{

    cout << "Value number " << (counter2 + 1) << " is " << foo[counter3] << endl;

    counter3--;

}

cout << endl;

return;
}


任何帮助,将不胜感激。谢谢!

最佳答案

storeArray(&foo [arraysize],arraysize);


这会将地址传递到foo的末尾

而是使用:

storeArray(foo, arraysize);


和:

void storeArray(int* foo, int arraysize)


同样适用于:


  flipArray(&foo [arraysize],arraysize);


编辑:刚注意到您#include <array>并使用C样式的数组。不是在#include <array>中定义的C ++ STL数组

关于c++ - 运行时检查失败#2 S C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36928734/

10-11 00:38