2014-01-20 100 views
0

我一直在試圖將兩個數組傳遞給一個函數,以便我可以比較它們,但是如何傳遞數組並開始比較行。我遇到錯誤,如不兼容的指針類型傳遞給類型const char ????這裏是我迄今爲止...有麻煩在頂部排序功能將兩個二維數組傳遞給函數進行比較

//

#define MAXROWS 30 
#define MAXCOLS 100 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

char topsort(char, int, char, int);  

int main(int argc, const char * argv[]) 
{ 
    //array for all of the word's in the file 
    char list[MAXROWS][MAXCOLS], line[MAXCOLS], constraint[MAXROWS][MAXCOLS]; 
    FILE *list_sort; 
    int listcols = 0, listrows = 0, concols = 0, conrows = 0; 

    //open the sequential access file and make sure its found 
    list_sort = fopen("/Volumes/JENN/cpma stuff/introcompsys/list sort.txt","r"); 
    if(list_sort == NULL){ 
     printf("can't open file"); 
     exit(EXIT_FAILURE); 
    } 

    while(fgets(line, sizeof(line), list_sort) != NULL) 
    { 
     if(index(line, ',') == NULL){ 
      for(listcols =0; listcols< strlen(line)-1 ;++listcols) { 
       list[listrows][listcols] = line[listcols]; 
      } 
      list[listrows][listcols] = '\0'; 
      printf("%s\n", list[listrows]); //print each row of the list to check 
      ++listrows; 
     } 
     else{ 
      for(concols =0; concols< strlen(line)-1 ;++concols) { 
       constraint[conrows][concols] = line[concols]; 
      } 
      constraint[conrows][concols] = '\0'; 
      printf("%s\n", constraint[conrows]); //print each row of the constraint to 
      //check 
      ++conrows; 
     } 

    } 
} 
char topsort(char s1[][MAXCOLS], int listrows, char s2[][MAXCOLS], int conrows){ 
char sorted[MAXROWS][MAXCOLS]; 

while(the constraint array is not empty){  //pseudocode 
    int second = char *strchr(s2, ‘,’+ 2); 
    for(int i = 0; i < listrows ; i++){ 
     for(int j = 0; j < conrows; j++){ 
      strcspn(s2[j][second], s1[i]); 
     } 
    } 
} 

}

+0

你的函數聲明在哪裏? – haccks

+0

請將警告/錯誤添加到問題中。 –

+0

包含所有錯誤消息,包括行號。也是示例輸入。 –

回答

-1

多個問題topsort IM的代碼。這不會起作用。

這是一個壞主意:

char s1[][MAXCOLS] 

要麼指定所有的尺寸大小,還是使用字符**。

這是錯誤的,原因有幾個。

int length = strlen(s2[][]); 

你不給任何數組索引,你傳遞一個char這需要一個char *的功能,並且使用的是在一個while循環長度,從來沒有改變它。

strlen(&s1) 

這是將char * * *傳遞給期望char *的函數。

strcspn(s2[i], s1[i]); 

將兩個字符傳遞給一個帶有char和char *的函數。另外你甚至不會看到結果。

+1

'char s1 [] [MAXCOLS]'是一個壞主意?在這種情況下,從'main'傳遞的參數(應該是)有一個簽名'list [MAXROWS] [MAXCOLS]','char ** s1'是一個壞主意,因爲數據是連續的,而不是一個指針數組。 –

+0

*或**在變量前面意味着什麼?該函數顯然不是「功能」....我不知道用於比較數組行中的值而不是指向它們的語法 – user3215644