2017-02-24 192 views
0

我的函數passwordschar**作爲輸入。我需要這個函數在一個位置放置一個特定的字符。我的程序崩潰,我不知道我做錯了,但我已經將範圍縮小到指針:指向Char陣列指針的指針

// The function 
int passwords(int num, int line, char** letters, char** ptrPassword, int length) { 
    char* password = *ptrPassword; 

    // Later in the code 
    password[location] = letters[line][0]; 
} 

這裏是主我的電話:

char** password; 
password = malloc(length * sizeof(char)); 
location = length; 

//call function 
printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, password, length)); 

我用指針不太經驗,有人可以協助嗎?

主營:

#include<stdio.h> 
#include<stdlib.h> 

int passwords(int num, int line, char** letters, char** ptrPassword, int length); 
int lengthOfString(char* string); 
void print(char** letters, int length); 

int location; 

int main(){ 
    int numCase; 
    scanf("%d", &numCase); 

    int i, length; 
    for(i = 0; i < numCase; i++){ 
     scanf("%d", &length); 
     int j; 
     char** letters = malloc(length*sizeof(char*)); 

     for(j = 0; j < length; j++){ 
      letters[j] = calloc(26, sizeof(char)); 
      scanf("%s", letters[j]); 
     } 
     int rank; 
     scanf("%d", &rank); 

     //print(letters, j); 


     char* password; 
     password = malloc(length * sizeof(char)); 
     location = length; 

     //call recursion 
     printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length)); 
    } 

    return 0; 
} 

整函數:

//recursive function 
int passwords(int num, int line, char** letters, char** ptrPassword, int length){ 
    char* password = *ptrPassword; 
    printf("Recursion #%d \n", line); 
    if(line == length-1){ 
     printf("Line is equal to length \n"); 
     if(num > lengthOfString(letters[line])){ 
      printf("IF \n"); 
      if(num % lengthOfString(letters[line]) == 0){ 
       password[location] = letters[line][lengthOfString(letters[line])]; 
      } 
      else{ 
       password[location] = letters[line][num % lengthOfString(letters[line]) - 1]; 
      } 
      printf("location: %d \n", location); 
      location--; 
      printf("Password is: %s \n", password); 
     } 
     else{ 
      printf("ELSE \n"); 
      if(num/lengthOfString(letters[line]) == 1){ 
       *password[location] = letters[line][0]; 
      } 
      else{ 
       printf("Alocation: %d \n", location); 
       password[location] = letters[line][num/lengthOfString(letters[line])]; 
      } 
      printf("Blocation: %d \n", location); 
      location--; 
      printf("Password is: %s \n", password); 
     } 
     return lengthOfString(letters[line]); 
    } 
    else{ 
     printf("Line is not equal to length \n"); 
     int scalar = passwords(num, ++line, letters, ptrPassword, length); 
     if (num > scalar){ 
      if(num % scalar == 0){ 
       password[location] = letters[line][lengthOfString(letters[line])]; 
      } 
      else{ 
       password[location] = letters[line][num % scalar - 1]; 
      } 
      location--; 
     } 
     else{ 
      if(num/scalar == 1){ 
       password[location] = letters[line][0]; 
      } 
      else{ 
       password[location] = letters[line][num/lengthOfString(letters[line])]; 
      } 
      location--; 
     } 
     return scalar * lengthOfString(letters[line]); 
    } 
} 
+0

你可以發佈你的整個程序嗎?不可能看到你在這裏發佈的內容有什麼問題。 –

+0

您是否使用過像gdb這樣的調試器?請張貼更多你的代碼。 – Jarvis

+0

您遇到崩潰的輸入是什麼?你的程序也有編譯錯誤,也許你的意思是'password [location]'而不是'* password [location]'。 – Jarvis

回答

1

改變這一行

printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length)); 

printf("Password at %d is %d \n", rank, passwords(rank, 0, letters, &password, length)); 

由於你的passwords函數返回一個整數,而不是一個字符串(char *)。編譯器警告也是如此。

+0

他應該也絕對不會傳遞指向指針的指針。 – Lundin