I'm using a pointer to an array in a function that reference a array in the main function. I've completely forgotten about pointers. I'm trying:
我在函数中使用指向数组的指针,该函数引用main函数中的数组。我完全忘记了指针。我尝试着:
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;
}
I'm getting errors but can't quite put a finger on what I'm screwing up here.
我收到了错误,但是我不能完全理解我在这里搞砸了什么。
He are the errors (sorry for not including them in the initial post):
password.cpp:7:14: error: two or more data types in declaration of ‘main’
password.cpp: In function ‘char* generator(char*)’:
password.cpp:31:3: error: expected unqualified-id before ‘}’ token
password.cpp:32:10: error: ‘result’ was not declared in this scope
thanks for any help.
谢谢你的帮助。
2 个解决方案
#1
1
First of all,i can tell you many reasons for the errors If you are using the exact program to compile,
首先,我可以告诉你许多错误的原因如果你使用确切的程序进行编译,
-
length
does not has an initialization长度没有初始化
-
The
signature
of the functionotherFunction
varies between where it is called and its definition函数otherFunction的签名在调用它的位置和它的定义之间变化
-
*array[i]
does not make any sense in the definition ofotherFunction
sincearray[i]
itself is a dereference operation* array [i]在otherFunction的定义中没有任何意义,因为array [i]本身是一个解除引用操作
I think this is what you are expecting
我想这就是你所期待的
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);
}
O/P:
array Before otherFunction
xyray After otherFunction
#2
0
You seem to display confusion in regards to pointer fundamentals. There are six erroneous lines in your eight lines of code. Which book are you reading?
你似乎在指针基础方面表现出混乱。你的八行代码中有六条错误的行。你正在读哪本书?
#1
1
First of all,i can tell you many reasons for the errors If you are using the exact program to compile,
首先,我可以告诉你许多错误的原因如果你使用确切的程序进行编译,
-
length
does not has an initialization长度没有初始化
-
The
signature
of the functionotherFunction
varies between where it is called and its definition函数otherFunction的签名在调用它的位置和它的定义之间变化
-
*array[i]
does not make any sense in the definition ofotherFunction
sincearray[i]
itself is a dereference operation* array [i]在otherFunction的定义中没有任何意义,因为array [i]本身是一个解除引用操作
I think this is what you are expecting
我想这就是你所期待的
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);
}
O/P:
array Before otherFunction
xyray After otherFunction
#2
0
You seem to display confusion in regards to pointer fundamentals. There are six erroneous lines in your eight lines of code. Which book are you reading?
你似乎在指针基础方面表现出混乱。你的八行代码中有六条错误的行。你正在读哪本书?