c++指针入门,用起来有点晕
指针与函数
/*下面的程序中,调用了findmax()函数,该函数寻找数组中的最大元素,返回该最大元素的地址,并将该元素的下标通过参数返回,编程实现findmax()函数。
#include <iostream>
using namespace std;
int * findmax( int * array, int size, int *index);
int main(void)
{
int a[10]={33,91,54,67,82,37,85,63,19,68};
int * maxaddr;
int idx;
maxaddr=findmax(a,sizeof(a)/sizeof(*a),&idx);
cout<<”the index of the maximum element is”<<idx<<endl
<<”the address is “<<maxaddr<<endl
<<”the maximum value is “<<a[idx]<<endl;
return 0;
}
*/
#include <iostream>
using namespace std;
int* findmax(int* array, int size, int* index);
int* maxaddr;
int main(void)
{
int a[10] = { 33,91,54,67,82,37,85,63,19,68 };
int idx;
maxaddr = findmax(a, sizeof(a) / sizeof(*a), &idx);
cout << "the index of the maximum element is" << idx << endl
<< "the address is " << maxaddr << endl
<< "the maximum value is " << a[idx] << endl;
return 0;
}
int* findmax(int* array, int size, int* idex) {
int max = 0;
for (int i = 0; i < 10; i++) {
if (array[i] > max) {
max = array[i];
idex = &i;
}
}
maxaddr = &max;
return 0;
}
异常,读取位置 0x000000FD78632E58 时发生访问冲突
指针被释放?野指针?指针没有赋初值?
指针的基本运用已经不太明白了,好晕啊