C++ 为数组的指针赋值
我在函数中使用指向数组的指针,该函数引用了主函数中的数组。我完全忘记了指针。我正在努力:C++ 为数组的指针赋值,c++,arrays,pointers,C++,Arrays,Pointers,我在函数中使用指向数组的指针,该函数引用了主函数中的数组。我完全忘记了指针。我正在努力: int main(void){ int length; char punct; string password; cout << "Enter length of passwords: " << endl; cin >> length; char array[length]; //run random password
int main(void){
int length;
char punct;
string password;
cout << "Enter length of passwords: " << endl;
cin >> length;
char array[length];
//run random password generator here
password = generator(*array);
cout << "Here is your password: " << endl;
return 0;
}
char* generator(char* array){
int counter = 0;
int random;
while(counter <= 8){
random = rand() % 200 + 32;
if(random >= 32 && random != 95 && random != 127)
char
}
return result;
}
array Before otherFunction
xyray After otherFunction
谢谢您的帮助。首先,如果您使用的是精确的程序进行编译,我可以告诉您错误的许多原因
array Before otherFunction
xyray After otherFunction
length
没有初始化array Before otherFunction
xyray After otherFunction
otherFunction
的signature
在调用位置和定义之间有所不同array Before otherFunction
xyray After otherFunction
*array[i]
在otherFunction
的定义中没有任何意义,因为array[i]
本身就是一个解引用操作array Before otherFunction
xyray After otherFunction
我想这就是你所期待的
char* otherFunction(char[] array)
{
array[0] = 'x';
array[1] = 'y';
return array;
}
int main()
{
int length =5;
char array[length] = "array";
printf("%s Before otherFunction",array);
char* newArray = otherFunction(array);
printf("%s After otherFunction",array);
}
array Before otherFunction
xyray After otherFunction
O/p:
array Before otherFunction
xyray After otherFunction
您似乎对指针的基本原理感到困惑。八行代码中有六行错误。你在读哪本书?我简直不知道从哪里开始-1“犯错误”不是很有用,会使问题对其他人毫无用处。解释标题中总结的错误是-1。复制并粘贴问题2中的错误。发帖前,搜索上述错误。如果你知道自己忘记了什么,通常最好先刷新一下记忆,然后再问别人,让自己看起来很无知。我在这里再次阅读了指针:我添加了原始代码,而不仅仅是摘要。谢谢你关于“看起来无知”的建议,但我认为“缺乏知识”可能是正确的措辞。我大部分时间都在另一个论坛上发帖,忘记了这个论坛上的礼仪,所以请接受我的道歉。是的,这是我在发帖前没有足够的参考资料而留下的悲伤记忆。我道歉。好的……这就是我想要的答案。非常感谢你。很多时候,当我跳到堆栈上时,我会尝试解释我的代码,以免给试图帮助我的人带来负担。这显然有时会适得其反,并导致一些人胡言乱语,发表粗鲁的评论。但我非常感谢先生或女士的帮助。@Verber社区对我们是有礼貌的,只要我们向他们提供更多关于我们心中问题的信息。nyways wc.:)我同意,这是我的错……但有一种体面的方式来处理事情,也有一种不自然的方式。一句简单的“请详细说明错误和你的问题”就足够了。我已经得到了-1或更少的问题,我已经提供了所有可用的信息和源代码。不是我需要我的代表号码来验证自己。我只是在玩C++今晚…不是我的强项。我真的很感谢你的帮助……那一句话让我感到很不舒服。我在力所能及的地方帮助人们,而不需要对他们进行任何攻击……不管他们知道多少。
array Before otherFunction
xyray After otherFunction