向函数传递指针

C++ Passing pointer to function

本文关键字:指针 函数      更新时间:2023-10-16

我有一个函数

    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);