我一直在尝试将两个数组传递到一个函数中,以便可以比较它们,但我在如何传递数组并开始比较行的语法方面遇到了问题。我收到诸如不兼容的指针类型传递给 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]);
}
}
}
}
最佳答案
topsort 存在多个问题。这是行不通的。
这是一个坏主意:
char s1[][MAXCOLS]
指定所有维度大小,或使用 char **。
由于多种原因,这是错误的。
int length = strlen(s2[][]);
您没有给出任何数组索引,您将一个 char 传递给一个采用 char * 的函数,并且您在 while 循环中使用 length 并且从不更改它。
strlen(&s1)
这是将 char * * * 传递给需要 char * 的函数。
strcspn(s2[i], s1[i]);
将两个字符传递给一个采用 char 和 char * 的函数。另外,您甚至看不到结果。
关于c - 将两个二维数组传递给函数进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21237705/