2015-04-23 111 views
-1

我想一個二維數組轉換爲雙指針,我發現了一個計算器的解決方案,但它不工作,如果我做一個打印它只是亂碼...轉換爲雙指針

我必須這樣做,因爲我後來調用的函數需要一個const char **,我不能改變它。

void test(const char** sl, int n) { 

    char s[n][20], t[20]; 
    int i, j, a; 

    for(i = 0; i < n; ++i) { 
     strcpy(s[i], sl[i]); 
    } 


    for (i = 1; i < n; i++) { 
     for (j = 1; j < n; j++) { 
     if (strverscmp(s[j - 1], s[j]) > 0) { 
      strcpy(t, s[j - 1]); 
      strcpy(s[j - 1], s[j]); 
      strcpy(s[j], t); 
     } 
     } 
    } 

    char *solutionPtrs[n]; 
    for (a = 1; a < n; a++) 
      solutionPtrs[n] = s[n]; 

    char **ptr = solutionPtrs; 


    printf("\nStrings in order are : "); 
    for (i = 0; i < n; i++) 
     printf("\n%s", ptr[i]); <-- just not what it should be. 

} 

有人能告訴我爲什麼它不起作用嗎?

回答

1

我認爲這裏有一個錯誤:

for (a = 1; a < n; a++) 
     solutionPtrs[n] = s[n]; 

您是不是要找:

for (a = 0; a < n; a++) 
     solutionPtrs[a] = s[a];