簡體   English   中英

C ++將指針傳遞給函數

[英]C++ Passing pointer to function

好,我有功能

    int main
{
   ....
   char *wordl=word();//wordl pointer is an array of characters
   ...
   gamewindow(wordl,length);
}


void gamewindow(char &wordl,int length);

我的問題是如何傳遞指針,以使傳遞的指針指向同一數組列表。我可以在gamewindow函數中通過wordl [i]訪問它。

從下面的評論中,實現word()

   char* word() 
    { int j=1988497762; cout<<j<<endl ; 
    static char original[25]; 
    int x;
     ifstream fin("out.txt"); 
    for (j=0;!fin.eof();j++) 
    fin.getline(original,25); 
    fin.close(); 
    srand ( (unsigned) time(NULL) ); 
    x = rand()%j; cout<<x<<"\n"; 
    cout<<rand()<<endl; 
    char c; 
    ifstream finn("out.txt"); 
    for (j=0; !finn.eof(); j++)
     { finn>>c; finn.getline(original,25); if (x==j) break; } 
    finn.close(); 
    return original; }

如果您真的只想像這樣使用它(而不使用std::vectorstd::string ),則需要將gamewindow更改為void gamewindow(char *wordl,int length); 然后,您可以使用word1[i]訪問char

最好使用std::stringstd::vector和其他標准容器 如果在C ++ 2011上,請使用std :: array

所以改為聲明

 void gamewindow(std::string&str);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM