给数组指针赋值

Assign a value to a pointer of an array

本文关键字:赋值 指针 数组      更新时间:2023-10-16

我在函数中使用指向数组的指针,该指针引用了主函数中的数组。我完全忘记了指针。我在:

    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

感谢您的帮助

首先,我可以告诉你很多错误的原因,如果你正在使用确切的程序来编译,

  1. length没有初始化

  2. 函数otherFunctionsignature在调用位置和定义之间变化

  3. *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

你似乎在指针基础方面显示混乱。你的八行代码中有六行是错误的。你在读哪本书?