-2

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

    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

谢谢你的帮助。

4

2 回答 2

1

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

  1. length没有初始化

  2. signature函数在otherFunction调用位置和定义之间有所不同

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

输出/输出:

array Before otherFunction
xyray After otherFunction
于 2013-01-19T04:51:11.053 回答
0

您似乎对指针基础知识感到困惑。你的八行代码中有六行错误。你在看哪本书?

于 2013-01-19T05:07:31.183 回答