我正确使用这个动态数组吗?

发布于 2024-12-05 08:42:29 字数 1923 浏览 1 评论 0原文

我需要创建一个动态数组来保存要从三个文件中读取的字符串。我是 C 新手,不太了解如何使用指针或分配内存。我想知道我是否正确声明了我的数组以及我的 calloc() 调用是否正确。我要使用的文件格式是:

word1
word2
word3 (and so on)

我只是假设文件中的单词不超过 50 个字符(包括 \0)。

最终我需要对它们进行排序,但在尝试之前我需要将它们放入一个数组中。感谢您能够提供的任何帮助。


这是我到目前为止所拥有的......

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

int countWords(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
    if (ch == '\n')
        count++;
}
return count;
}


int main(void){

int i;
int wordCount = 0;
int stringLen = 50;

FILE *inFile;

inFile = fopen("american0.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

inFile = fopen("american1.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

inFile = fopen("american2.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

printf("%d\n", wordCount);


char **wordList = (char **) calloc(wordCount, wordCount * sizeof(char));
for (i = 0; i < wordCount; i++){
    wordList[i] = (char *) calloc(stringLen, stringLen * sizeof(char));
}

char ch;
int currentWord = 0;
int currentWordIndex = 0;
inFile = fopen("american0.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}
inFile = fopen("american1.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}
inFile = fopen("american2.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}

printf("%s\n", wordList[57]);
for (i = 0; i < wordCount; i++){
    free(wordList[i]);}

free(wordList);
return 0;
}

I need to create a dynamic array to hold strings that I am to read in from three files. I am new to C and I don't really understand how to use pointers or allocate memory. I would like to know if I am declaring my array correctly and if my calloc() calls are correct. The format for the file I am to use is:

word1
word2
word3 (and so on)

I'm just to assume that the words from the files are no longer than 50 characters (including \0).

Eventually I will need to sort them, but I need to get them into an array before I try that. Thanks for any help you may be able to give.


Here is what I have so far...

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

int countWords(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
    if (ch == '\n')
        count++;
}
return count;
}


int main(void){

int i;
int wordCount = 0;
int stringLen = 50;

FILE *inFile;

inFile = fopen("american0.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

inFile = fopen("american1.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

inFile = fopen("american2.txt", "r");
wordCount += countWords(inFile);
fclose(inFile);

printf("%d\n", wordCount);


char **wordList = (char **) calloc(wordCount, wordCount * sizeof(char));
for (i = 0; i < wordCount; i++){
    wordList[i] = (char *) calloc(stringLen, stringLen * sizeof(char));
}

char ch;
int currentWord = 0;
int currentWordIndex = 0;
inFile = fopen("american0.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}
inFile = fopen("american1.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}
inFile = fopen("american2.txt", "r");
while ((ch = fgetc(inFile)) != EOF){
    if (ch == '\n'){
        currentWord++;
        currentWordIndex = 0;
    }
    else
        wordList[currentWord][currentWordIndex] = ch;
}

printf("%s\n", wordList[57]);
for (i = 0; i < wordCount; i++){
    free(wordList[i]);}

free(wordList);
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

  • 您不需要对 calloc 的返回值进行强制转换。 C 语言指定 void* 类型的值与任何类型的对象指针兼容。添加强制转换可能会隐藏不包含声明 calloc 的标头的错误。 在 C++ 中,规则有所不同。

  • 函数 calloc() 有两个参数:要分配的元素数量和每个元素的大小

    • 在第一个 calloc 中,您尝试分配奇怪大小的 wordCount 元素。我喜欢使用对象本身作为 sizeof 运算符的操作数
    • 的 操作数

    • 在第二个 calloc 中,您尝试分配 50 个元素,每个元素的大小为 50。但您只需要每个 wordCount 中有 1 个元素,对吧?另外,根据定义,sizeof (char)1,因此它不会给您带来任何乘以它的东西。

尝试这样

char **wordList = calloc(wordCount, sizeof *wordlist);
for (i = 0; i < wordCount; i++) {
    wordList[i] = calloc(1, stringLen);
}
  • You don't need the casts for the return value of calloc. The C language specifies that a value of type void* is compatible with any type of pointer to object. Adding the cast may hide the error of not including the header where calloc is declared. In C++ the rules are different.

  • The function calloc() takes two arguments: the number of elements to allocate and the size of each one

    • In the first calloc you were trying to allocate wordCount elements of a strange size. I like to use the object itself as operand to the sizeof operator
    • In the 2nd calloc you were trying to allocate 50 elements of size 50 each. But you only want 1 element in each wordCount, right? Also sizeof (char) is, by definition, 1 so it doesn't buy you anything to multiply by it.

Try like this

char **wordList = calloc(wordCount, sizeof *wordlist);
for (i = 0; i < wordCount; i++) {
    wordList[i] = calloc(1, stringLen);
}
残龙傲雪 2024-12-12 08:42:29

尝试使用链表数据结构。

样本:
http://www.macs.hw.ac.uk/ ~rjp/Coursewww/Cwww/linklist.html

这更适合您的需求。

Try using linked-list data structure.

Sample:
http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html

this suits better to your need.

多彩岁月 2024-12-12 08:42:29

在 sizeof() 中,您必须使用要分配的类型。指向 char 的指针与 char 本身是不同的类型,并且可能(并且在大多数情况下)具有不同的大小。例如:

char **wordList = (char **) calloc(wordCount, sizeof(char*));

另外,您不需要将指针的大小乘以字数,calloc 已经为您做到了这一点。你也可以这样做:

char **wordList = (char **) malloc(wordCount * sizeof(char*));

In sizeof() you must use the type that you're allocating. A pointer to char is a different type than char itself and may (and under most circumstances does) have a different size. For example:

char **wordList = (char **) calloc(wordCount, sizeof(char*));

Also, you don't need to multiply the size of the pointer by the word count, calloc already does that for you. You could also do it this way:

char **wordList = (char **) malloc(wordCount * sizeof(char*));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文