给数组指针赋值
Assign a value to a pointer of an array
我在函数中使用指向数组的指针,该指针引用了主函数中的数组。我完全忘记了指针。我在:
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;
}
我得到了错误,但不能完全指出我在这里搞砸了什么。
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
感谢您的帮助
首先,我可以告诉你很多错误的原因,如果你正在使用确切的程序来编译,
-
length
没有初始化 -
函数
otherFunction
的signature
在调用位置和定义之间变化 -
*array[i]
在otherFunction
的定义中没有任何意义,因为array[i]
本身是一个解引用操作
我想这是你所期望的
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
你似乎在指针基础方面显示混乱。你的八行代码中有六行是错误的。你在读哪本书?
相关文章:
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 关于 c++ 函数中指针赋值的简单问题
- VkSurfaceKHR 指针的值在函数调用后更改,无需任何显式赋值
- 在 Arduino 中为 2D 指针数组赋值
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 为什么通过指针赋值常量没有效果?
- 通过 lambda 在空指针处赋值
- 指针到指针算术:赋值
- 复制构造函数中的指针赋值
- 如何将值从指针永久赋值到变量
- 通过智能指针向类成员进行赋值
- 为什么在取消引用的指向接口的指针上使用赋值运算符不是编译器错误
- 将函数赋值给函数指针,常量参数正确性
- 为指针指向的地址赋值?C++
- C++ 包含唯一指针成员变量的类的赋值运算符
- 在类数据成员中赋值指针
- 有没有一种方法可以自动地赋值指针并增加计数器,而不需要锁?
- 赋值指针数组
- 如何在c++中赋值指针给this