c - 分配 - 指针到指针

标签 c

我想寻求分配方面的帮助....我把这个作业带到了学校......我必须编写程序来加载一个 G 矩阵和第二个 G 矩阵,并搜索第二个 G 矩阵以获取存在数第一个 G 矩阵的......但是,当我尝试运行我的程序时,我收到了段错误消息......提前致谢。 例如程序应该如何工作....

...

输入想要的 g 矩阵的行数: 3个 输入想要的g矩阵:
121212
212121
121212
待搜索的G矩阵:
12121212121212
21212121212121
12121212123212
21212121212121
12121212121212
G矩阵找到8次。

...

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char * get_line(void) // get line
{
    char * string;
    if((string = (char *)malloc(100 * sizeof(char))) == NULL)
    {
        printf("Nedostatek pameti.\n"); // not enough memory
        exit(1);
    }
    int i = 0;
    while((string[i] = fgetc(stdin)) != '\n')
    {
        i++;
        if((i % 100) == 0)
        {
            if((string = (char *)realloc(string, 100 * ( i - 1 ) * sizeof(char))) == NULL)
            {
                printf("Nedostatek pameti.\n"); // not enough memory
                exit(1);
            }
        }
    }
    return ( string );
}

char ** get_wanted_g_matrix(int pocetradek /*number of lines*/) // get wanted g matrix
{
    char ** string;
    printf("Zadejte hledanou matici:\n");
    int i = 0;
    if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)
    {
        printf("Nedostatek pameti.\n"); // not enough memory
        exit(1);
    }
    while(i <= (pocetradek - 1))
    {
        string[i] = get_line();
        if((i > 1) && (*string[i-1] != strlen(*string[i])))
        {
               printf("Nespravny vstup.\n"); // not enough memory
               exit(1);
        }
        printf("%s", string[i]);
        i++;
        if((i % 100) == 0)
        {
            if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)
            {
                printf("Nedostatek pameti.\n"); // not enough memory
                exit(1);
            }
        }
    }
    return (string);
}

int get_number_of_lines(void) // get number of lines
{
    int number_of_lines;
    printf("Zadejte pocet radek hledane matice:\n"); // enter the number of lines of wanted g matrix
    if(scanf("%d", &number_of_lines) != 1)
    {
        printf("Nespravny vstup.\n"); // Error
        exit(1);
    }
    return ( number_of_lines );
}

char ** get_searched_g_matrix(void) // get wanted g matrix
{
    char ** string;
    printf("Matice, ktera bude prohledana:\n"); // G matrix to be searched
    int i = 0;
    if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)
    {
        printf("Nedostatek pameti.\n"); // not enough memory
        exit(1);
    }
    while(!feof(stdin))
    {
        string[i] = get_line();
        if((i > 1) && (*string[i-1] != strlen(*string[i])))
        {
               printf("Nespravny vstup.\n"); // error
               exit(1);
        }
        printf("%s", string[i]);
        i++;
        if((i % 100) == 0)
        {
            if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)
            {
                printf("Nedostatek pameti.\n"); // not enough memory
                exit(1);
            }
        }
    }
    if(feof(stdin))
    {
    return string;
    }
}
int search( char ** string1, char ** string2 ) // search
{
    int string1width = strlen(*string1[0]);
    int string2width = strlen(*string2[0]);
    int string2height = strlen(**string2);
    int number_of_lines = get_number_of_lines();
    unsigned int g = 0, h = 0, i2, j2, l = 0, i = 0, j;
            while( i <= (string2height - 2) )
            {
                j = 0;
                while( j <= string2width - 2 )
                {
                    g = 0; h = 0;
                    if(string2[i][j] == string1[g][h])
                    {
                        i2 = i;
                        while((g <= number_of_lines - 1) && (i2 <= string2height - 2))
                        {
                            j2 = j; h = 1;
                            while(((string2[i2][j2] == string1[g][h]) && (j2 <= string2height - 2)) && (h <= string1width - 2))
                            {
                                j2++;
                                h++;
                            }
                            if(h != string1width - 1)
                            {
                                break;
                            }
                            if(g == number_of_lines - 1)
                            {
                                l++;
                                break;
                            }
                            i2++;
                            g++;
                        }
                    }
                    j++;
                }
                i++;
            }
            return ( l );
}

int main(void)
{
    char ** string1;
    char ** string2;
    int number_of_lines = get_number_of_lines();
    string1 = get_wanted_g_matrix(number_of_lines);
    string2 = get_searched_g_matrix();

    if(feof(stdin))
    {
                printf("Matice nalezena %d krat.\n", search( ** string1, **string2 )); // G matrix found %d times.
    }

    return 0;
}

最佳答案

在这段代码中:

char ** get_wanted_g_matrix(int pocetradek /*number of lines*/) // get wanted g matrix
{
    char ** string;
    printf("Zadejte hledanou matici:\n");
    int i = 0;
    if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)

您取消引用 string ,但它未初始化,因此您正在写入内存中的随机位置。改变* string只是string .这同样适用于这里:

if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)

..和get_searched_g_matrix()中的相应行

在这一行中:

    if((i > 1) && (*string[i-1] != strlen(*string[i])))

您传递的是 charstrlen() ,当你应该通过 char * .我怀疑你的意思只是 strlen(string[i]) , 但那句话似乎有些荒谬。同样的问题在get_searched_g_matrix()以及对 strlen() 的前三个调用在search() .

你的 get_searched_g_matrix()可以在没有返回值的情况下掉到最后 - 你需要考虑如果 feof(stdin) 返回什么不是真的。

main() , 您调用 search()通过 char值,但函数需要 char ** .你可能是说:

printf("Matice nalezena %d krat.\n", search( string1, string2 ));

(以上内容不足以修复您的代码 - 您似乎也有一些逻辑问题。但这是一个必要的开始。)

将来,您应该在启用更高级别警告的情况下编译您的代码,然后修复编译器识别的问题(如果您使用的是 gcc,请使用 -Wall 进行编译)。

关于c - 分配 - 指针到指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4236261/

相关文章:

c - 如何使用 Monotouch 强制退出 iOS 应用程序?

java - 在 Java 和 C 文件中创建属性

c - 是否可以在winsock中将tcp服务器连接到udp客户端

c - 自动删除双向链表中的元素

c++ - 重写静态库中的 C 函数

c - 使用strtok将一个简单的方程存储到一个数组中,然后进行运算?

c - 从 C 执行程序

CodeWarrior 编译器警告 : "Possible loss of data"

在父级中捕获子级错误

c - 在C中查找数组中最大值的索引