I am trying to cout
an array in my C++ code:
#include <iostream>
using namespace std;
void fun(int *a) {
a[3] = 1;
for (int i = 0; i<sizeof(a)/sizeof(a[0]); ++i) {
cout << a[i] << endl;
}
}
int main(int *arry) {
fun(arry);
system("pause");
}
I got error Exception thrown: write access violation.
**a** was 0x1. occurred
at Line 5 by using VS2017.
But if I write "something normally" like below, it is no problem:
void fun() {
int a[3] = { 1 };
for (int i = 0; i<sizeof(a)/sizeof(a[0]); ++i) {
cout << a[i] << endl;
}
}
I think both snippets are doing the similar thing, but why the 1st isn't correct? I am confused now, I appreciate anyone can offer some suggestion? Thanks in advance!