为数组的指针赋值

时间:2013-01-19 04:48:16

标签: c++ arrays pointers

我在函数中使用指向数组的指针,该函数引用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;
   }

我遇到了错误,但无法完全理解我在这里搞砸了什么。

  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

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

  1. length没有初始化

  2. 函数signature的{​​{1}}在调用位置和定义之间有所不同

  3. otherFunction*array[i]的定义中没有任何意义,因为otherFunction本身是一个解除引用操作

  4. 我认为这是你所期待的

    array[i]

    O / P:

        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);
        }
    

答案 1 :(得分:0)

你似乎在指针基础方面表现出混乱。您的八行代码中有六条错误的行。你正在读哪本书?