[英]passing two 2-d arrays into a function for comparison
我一直试图将两个数组传递给一个函数,以便可以比较它们,但是我在如何传递数组并开始比较行的语法方面遇到麻烦。 我遇到错误,例如将不兼容的指针类型传递给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]);
将两个字符传递给需要一个字符和一个字符*的函数。 另外,您甚至看不到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.