c - 动态分配内存中指针的访问值

标签 c arrays pointers

我的任务是从文本文件中读取单词并将它们存储在字符数组中,字符数组存储在 char* 数组中。这些数组中的所有内存都需要动态分配。

我正在做的是使用 fscanf() 读取每个单词并将其存储到变量 str 中。然后我计算 str 中单词的长度,并动态分配内存以将 str 的值存储在字符数组 new_word 中。 new_word 然后被插入到名为 wordschar* 数组中。当 words 空间不足时,我将其大小加倍并继续。

我的问题在于从第 62 行开始的注释代码。稍后我需要从 words 中读取这些词,所以我正在测试我访问指针及其值的能力.我可以很好地索引 new_word(在上面的行中),但是当我将 new_word 存储在 words 中并尝试从 words 中读取时,我收到以下错误:

hw1.c:63:25: 错误:下标值不是数组、指针或 vector while (*(words[count])[k] != '\0'){

第 63 和 64 行。我知道它与取消引用指针有关,但我尝试了很多变体但没有成功。我该如何解决这个问题?

代码如下:

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


int main(int argc, char* argv[]){

    if (argc != 3){
        fprintf(stderr, "Incorrect number of arguments\n");
        exit(1);
    }

    char* infile = argv[1];
    FILE* finp = fopen(infile, "r");
    if (finp == NULL){
        fprintf(stderr, "Unable to open input file\n");
        exit(1);
    }

    char* prefix = argv[2];

    int count = 0;
    int size = 20;
    char* words = calloc(size, sizeof(char));
    printf("Allocated initial array of 20 character pointers.\n");
    char* str = malloc(30*sizeof(char));

    while (fscanf(finp, "%s", str) == 1){

        if (count == size){
            words = realloc(words, 2 * size);
            size *= 2;
            printf("Reallocated array of %d character pointers.\n", size);
        }

        int i = 0;
        while (str[i] != '\0'){
            i++;
        }

        char* new_word = malloc((i+1)*sizeof(char));

        int j = 0;
        while (str[j] != '\0'){
            new_word[j] = str[j];
            j++;
        }

        new_word[j] = '\0';

        int k = 0;

        while (new_word[k] != '\0'){
            printf("%c", new_word[k]);
            k++;
        }

        printf("\n");

        words[count] = *new_word;

        /*k = 0;
        while (*(words[count])[k] != '\0'){
            printf("%c", *(words[count])[k]);
            k++;
        }

        printf("\n");*/

        count++;

    }


}

最佳答案

好的,稍微剖析一下:

char* words = calloc(size, sizeof(char));

这应该是:

char **words = calloc(size, sizeof(char *));

为什么?你在这里想要的是一个指向指向 char 的指针数组的指针...... words 指向第一个 char *,它指向你的第一个“字符串”。


char* str = malloc(30*sizeof(char));

while (fscanf(finp, "%s", str) == 1){

此处缓冲区溢出。如果您将缓冲区定义为不容纳更多字符,请确保最多读取 30 个字符。顺便说一句,只是为了约定,调用您的缓冲区 bufferbuf(不是 str),实际上没有必要动态分配它。提示:为 fscanf() 使用字段大小,或者更好的是,使用一些其他函数,如 fgets()


    if (count == size){
        words = realloc(words, 2 * size);
        size *= 2;
        printf("Reallocated array of %d character pointers.\n", size);
    }

这里的realloc不会起作用,应该阅读

        words = realloc(words, 2 * size * sizeof(char *));

您需要乘以单个元素的大小,在本例中,它是指向 char 的指针。


不能保证这将是所有错误,但可能是最重要的错误。在旁注中,strlen()strncpy() 将帮助您停止编写不必要的代码。

关于c - 动态分配内存中指针的访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465658/

相关文章:

c - 如何解决C中无法解决的外部错误

java - 将伪代码转换为 Scala 时遇到问题

php - 如何用我自己的文本替换某些 PHP 值?

javascript - 如何访问多维数组(像这样)?

c - 链接头文件

c++ - 指向类的私有(private)数据成员的指针

c - timer_create 和timer_settime 生成段错误

c - 尝试让 int 变量数字等于字符串

c++ - 关于C++指针

c - 关于 switch-case 的一些不清楚的地方