首页 > 试题广场 >

有如下C++代码: #include ...

[单选题]
有如下C++代码:
#include <iostream>
using namespace std;
void fun(int *p1, int *p2, int *t)
{
	t = (int *)malloc(sizeof(int));
	*t = *p1 + *(p2++);
}
int main()
{
	int a[2] = { 1,2 };
	int b[2] = { 10,20 };
	int *t = a;
	fun(a, b, t);
	cout << *t << endl;
}
输出是多少?
  • 11
  • 10
  • 1
  • 2
  • 12
这个题基础不扎实确实很容易出错,我们经常说传值的时候形参是实参的一份拷贝,所以我们要对实参进行更改,要传地址。这个题就出现了传指针,很容易认为结果是11,但是当指针作为值传进去的时候我们想更改其指向空间的值我们这时候要传二级指针。
如果想得到11应该
#include <iostream>
using namespace std;
void fun(int *p1, int *p2, int **t)
{
	*t = (int *)malloc(sizeof(int));
	**t = *p1 + *(p2++);
}
int main()
{
	int a[2] = { 1,2 };
	int b[2] = { 10,20 };
	int *t = a;
	fun(a, b, &t);
	cout << *t << endl;
	system("pause");
	return 0;
}


发表于 2020-08-11 12:27:49 回复(5)
fun()函数中重新为 it 申请了地址,跟main函数中it的地址不一致了,所以fun中的it的值不能传给main函数里的it。
发表于 2020-08-09 11:21:06 回复(0)
fun函数中形参只是实参的一份拷贝,也就是说有两个指针指向a,一个是main函数中的,一个是fun函数中的,这两个是不一样的。而fun函数中的指针被重新申请了地址,这两个指针指向的就不再是同一块内存了。因此改变fun函数中的拷贝,不会影响main函数中的t。
发表于 2020-09-08 08:51:02 回复(0)
#include <iostream>
using namespace std;
int fun(int *p1, int *p2, int *t)
{
	t = (int *)malloc(sizeof(int));  //t指针申请了新的空间,不再指向a数组
	*t = *p1 + *(p2++); 
	return *t;
}
int main()
{
	int a[2] = { 1,2 };
	int b[2] = { 10,20 };
	int *t = a;
	fun(a, b, t);
	cout << *t << endl;  //输出a[0], 1
	cout << fun(a, b, t) << endl;  //输出11
}

发表于 2021-11-16 09:02:02 回复(2)
#include <iostream>
using namespace std;
void fun(int *p1, int *p2, int *t)
{
    t = (int *)malloc(sizeof(int));
    *t = *p1 + *(p2++);
}
int main()
{
    int a[2] = { 1,2 };
    int b[2] = { 10,20 };
    int *t = a;
    fun(a, b, t);
    cout << *t << endl;
}

main里面的t和fun里面的t是两个不同的变量,调用fun传参时只不过是将前者的值拷贝给了后者,在fun中对t的任何操作都不会影响main中的t,所以cout << *t << endl;还是输出t指向的原来的值,即1

发表于 2022-02-25 11:07:38 回复(0)
我丢上当了,虽然传的是指针,然而这是值拷贝,实参t和形参t是两个不同的指针指向同一个数组,后面再改变形参t指向的内存跟实参t没任何关系
发表于 2022-06-22 11:01:33 回复(0)
malloc后的赋值操作,改变了t的地址,导致后面对t的操作不会影响到a数组

发表于 2023-03-05 11:33:18 回复(0)
#include <iostream>
using namespace std;
void fun(int* p1, int* p2, int* t)
{
    cout << "p1: " << p1 << endl;
    cout << "*p1: " <<* p1 << endl;
    cout << "p2: " << p2 << endl;
    cout << "*p2: " << *p2 << endl;
    cout <<"t: " << t << endl;
    cout << "*t: " << *t << endl;
    t = (int*)malloc(sizeof(int));  // 为t申请了一个新的地址,t其实也算一个局部变量
                                    // 因为地址不同,所以即使修改了t所存储的地址也对原来的t存储的地址的值也无影响
    cout << "t: " << t << endl;
    cout << "*t: " << *t << endl;   // 如果申请了一个新的地址,因为没有赋值所以是随机值
    cout << "p1: " << p1 << endl;
    cout << "*p1: " << *p1 << endl;
    *t = *p1 + *(p2++); // 给这个新申请的地址赋值,这个认定的只是地址而不是一个变量,也就是只是认定t所代表的地址
                        // 如果t改变的话,则*t只会对t修改后的地址进行值的修改
}
int main()
{
    int a[2] = { 1,2 };
    cout << "a: " << a << endl;
    cout << "*a: " << *a << endl;
    int b[2] = { 10,20 };
    cout << "b: " << b << endl;
    cout << "*b: " << *b << endl;
    int* t = a;
    cout << "t: " << t << endl;
    cout << "*t: " << *t << endl;
    fun(a, b, t);
    cout << *t << endl; // 输出为1,如果想输出11可以去掉malloc这一句,或者fun传入二级指针
}

发表于 2023-01-18 17:02:03 回复(0)
大方地说出我的错误答案21,居然没有这个选项!
踩了两个坑点,第一个是大家都注意到的坑点,函数传递的是形参的值,并不是地址,所以原变量数值不会改变,因为函数中已经把给形参重新分配内存地址了,不会影响传入变量的值。还有一个坑点需要注意的是函数中的自增用的是p2++,是先执行语句再进行自增1,所以函数中的*t结果是1+10,而不是1+20。
发表于 2022-04-03 11:42:48 回复(0)
指针传递本质上还是值传递,只不过这里的值是一个地址,引用传递才是真正的地址传递
发表于 2022-03-29 11:00:01 回复(0)
main函数中t传入不正确,应该传入二级指针,或者引用传参
发表于 2021-02-08 07:46:00 回复(0)