不行啊,对了,我的数组是const int a[]={1,5,3,2,8}和一个指针变量int* ptr,
//指针用const #include <iostream> #include <cstdlib> #define for if(0);else for using std::cout; using std::cin; using std::endl;int main(int argc, char *argv[]) {const int a[]={1,5,3,2,8}; const int *ptr;ptr = a;cout<<*(ptr+1)<<endl; // -> 5 system("pause"); return 0; }
const int a[] ={ 1, 2, 3 ,4 , 5}; const int * ptr; ptr = a; int b = ptr[1];
那么ptr+1 = &a[1];
依次类推
#include <iostream>
#include <cstdlib>
#define for if(0);else for
using std::cout;
using std::cin;
using std::endl;int main(int argc, char *argv[])
{const int a[]={1,5,3,2,8};
const int *ptr;ptr = a;cout<<*(ptr+1)<<endl;
// -> 5 system("pause");
return 0;
}
const int * ptr; ptr = a;
int b = ptr[1];