c - 将两个二维数组传递给函数进行比较

标签 c arrays comparison compare

我一直在尝试将两个数组传递到一个函数中,以便可以比较它们,但我在如何传递数组并开始比较行的语法方面遇到了问题。我收到诸如不兼容的指针类型传递给 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/

相关文章:

c - 分配内存中的空白空间会发生什么?

编译 libxml2 得到 "error: storage size of ‘hints’ 未知”

c - 错误消息显示为 'struct IteratorG' 没有名为 next 的成员'

PHP语法问题

set - perl 6 集合操作中用户定义的比较函数

c - C 中函数参数的放置

c - 编写一个函数来打印存储在数组中的字符串

php - 从 SQL 查询重构数组,以便我可以回显正确的值

objective-c - 在 Objective-C 中选择 NSArray 的随机元素

ruby - 如何将字符串与 Ruby 中的命令提示符输入进行比较