繁体   English   中英

指向指针的c ++指针和接收指向指针的调用函数

[英]c++ pointer to pointer and call function that receive pointer to pointer

问题是,如果我们已经在使用使用pointer to pointer操作的函数,该如何调用请求pointer to pointer的函数。 我尝试了一些变体,但所有变体均编译时出错(SIGSEGV) http://rextester.com/ZSW68923

void test_sessiontwo ( int **ptr_to_ptr )
{
    std::cout << *ptr_to_ptr;    
}

void test_session ( int **ptr_to_ptr )
{
    std::cout << "Hello, world2!\n" << *ptr_to_ptr;    
    **ptr_to_ptr = 20;
     test_sessiontwo   ( ptr_to_ptr ); // ERROR. HOW TO CALL THIS???
}

int main()
{
    int* ptr_int;
    *ptr_int = 10;
    test_session ( &ptr_int );   

    std::cout << "Hello, world!\n";
}

此代码段

   int* ptr_int;
    *ptr_int = 10;

已经错了。 指针ptr_int未初始化。 因此,该程序具有未定义的行为。

要测试您的代码,您可以编写例如

int main()
{
    int* ptr_int = new int( 10 );
    test_session ( &ptr_int );   

    std::cout << "Hello, world!\n";

    delete ptr_int;
}

尝试

int i=10;
int *ptr_int = &i;
test_session(&ptr_int);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM