向函数传递指针
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::vector
或std::string
),您需要将gamewindow
更改为void gamewindow(char *wordl,int length);
,然后您可以使用word1[i]
访问char
。
最好使用std::string
、std::vector
等标准容器。如果是c++ 2011,可以使用std::array
所以声明
void gamewindow(std::string&str);
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址