Search code examples
c++function-pointerspass-by-referencepointer-to-array

C++ Passing pointer to function


OK i have a function

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


void gamewindow(char &wordl,int length);

My question is how to pass pointer such that passed pointer points to the same array list.. and i can access it by wordl[i] in gamewindow function.

From the comment below, implementation of 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; }

Solution

  • If you really want to use it like this only (and not use std::vector or std::string) you need change gamewindow to void gamewindow(char *wordl,int length); Then you can access char using word1[i].