程序为#include <iostream> #include <string> #include <cmath> #include <iomanip> #include <algorithm> #include <conio.h> using namespace std; void test(int *A[]) { for(int a=0;a<2;a++) { cout<<A[a]<<endl; } } int main() { int b=3; int *a[3]={0, &b}; test(a); }为什么在dev c++上输出是0 0x70fe1c -------------------------------- Process exited after 0.02074 seconds with return value 0 请按任意键继续. . .
时间: 2024-03-10 14:47:24 浏览: 24
在这段代码中,定义了一个数组指针a,类型为int *a[3],并将它初始化为{0, &b}。这意味着数组指针a的第一个元素a[0]被初始化为0,第二个元素a[1]被初始化为变量b的地址,而第三个元素a[2]则没有被初始化,其值为0或未定义,这可能会导致程序出现未知的错误。
在函数test中,传入的参数A是一个数组指针,类型为int *A[]。在循环中,打印出数组指针中的两个元素,即a[0]和a[1]所指向的地址。由于a[2]未定义,因此在循环中没有被输出。
在Dev-C++中输出0和0x70fe1c的原因是,a[0]被初始化为0,因此输出0;而a[1]被初始化为变量b的地址,即0x70fe1c。此外,程序运行正常,返回值为0。
如果要输出0和3,可以将数组指针a的长度改为2,并将其初始化为{0, &b}。修改后的代码如下:
```
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <conio.h>
using namespace std;
void test(int *A[]) {
for(int a=0;a<2;a++) {
cout<<*A[a]<<endl;
}
}
int main() {
int b=3;
int *a[2]={0, &b}; // 将a[0]赋值为0,a[1]赋值为b的地址
test(a);
}
```
在这个修改后的代码中,我们将数组指针a的长度改为2,并将其初始化为{0, &b}。这样,在调用test函数时,test函数中循环打印的第一个元素就是0,第二个元素就是变量b的值3。