【问题标题】:Trying to scan a list of names into a double pointer试图将名称列表扫描成双指针
【发布时间】:2014-06-15 23:53:38
【问题描述】:

我不知道这里出了什么问题。尝试扫描名称列表: 鲍勃 蒂姆 托尼 艾莉森 吉姆

等等。进入双指针**字符串,但我不断收到段错误,看不到在哪里。

void insert_data(char **strings, const char *filename, int size)
{
    int j = 0;
    FILE* file = fopen(filename, "r");
    if(file == NULL)
    {
            printf("File could not be opened");
            return;
    }

    for(j=0; j<size; j++)
    {
            fscanf(file,"%s", strings[j]);
            printf("%s\n", strings[j]);
    }
    fclose(file);
}

我有一个单独的函数来分配内存,但它仍然存在段错误

void allocate(char ***strings, int size)
{
    strings = malloc(size * sizeof(char*));
    if(strings == NULL)
    {
            printf("Could not allocate memory\n");
    }

    int i;
    for(i=0;i<size;i++)
    {
            *(strings+i) = malloc(MAX_STRING_LEN * sizeof(char));
            if(strings == NULL)
            {
                printf("Could not allocate memory\n");
            }
    }
}

【问题讨论】:

  • 无法重现问题。
  • 问题可能出在调用它的代码中。这里有一些小问题,例如没有测试 fscanf() 的结果——你应该检查它是否返回 1,如果没有则退出循环——但总的来说它是合理的代码。请注意,您需要传递一个指针数组,而不是指向 2D 数组的指针,以使其工作。
  • 你的allocate函数错误。

标签: c string scanf


【解决方案1】:

你的功能的核心或多或少是健全的;问题更可能在于您调用它的方式而不是函数本身。以下代码仅对函数中的代码进行了少量修复,可以正常工作。

#include <stdio.h>

static int insert_data(char **strings, const char *filename, int size)
{
    int j = 0;
    FILE *file = fopen(filename, "r");
    if (file == NULL)
    {
        fprintf(stderr, "File %s could not be opened\n", filename);
        return 0;
    }

    for (j = 0; j < size; j++)
    {
        if (fscanf(file, "%s", strings[j]) != 1)
            return j;
        printf("%s\n", strings[j]);
    }
    fclose(file);
    return size;
}

int main(int argc, char **argv)
{
    char data[10][20];
    char *str[10];
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s file\n", argv[0]);
        return 1;
    }
    for (int i = 0; i < 10; i++)
        str[i] = data[i];
    int n = insert_data(str, argv[1], 10);
    for (int i = 0; i < n; i++)
        printf("%d: [%s] [%s]\n", i, str[i], data[i]);

    // Invalid - incompatible pointer type!
    // int m = insert_data(data, "data2", 10);

    return 0;
}

请注意,给定函数原型,您必须传递一个指针数组,而不是试图传递一个指向二维字符数组的指针。如果您尝试滥用该函数,编译器会发出警告。

【讨论】:

  • 有人愿意解释他们的反对票吗?大概不会。该死的匿名懦夫!
【解决方案2】:

改成

#define _S(x) #x
#define S(x) _S(x)

void insert_data(char **strings, const char *filename, int size)
{
    int j = 0;
    FILE* file = fopen(filename, "r");
    if(file == NULL)
    {
            printf("File could not be opened");
            return;
    }

    for(j=0; j<size; j++)
    {
            fscanf(file, "%" S(MAX_STRING_LEN) "s", strings[j]);//E.G. #define MAX_STRING_len 64
            printf("%s\n", strings[j]);
    }
    fclose(file);
}

void allocate(char ***strings, int size)
{
    *strings = malloc(size * sizeof(char*));
    if(*strings == NULL)
    {
            printf("Could not allocate memory\n");
            return ;
    }

    int i;
    for(i=0;i<size;i++)
    {
            (*strings)[i] = malloc((MAX_STRING_LEN+1) * sizeof(char));
            if((*strings)[i] == NULL)
            {
                printf("Could not allocate memory\n");
                return ;
            }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多