簡體   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