我正确使用这个动态数组吗?
我需要创建一个动态数组来保存要从三个文件中读取的字符串。我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要对
calloc
的返回值进行强制转换。 C 语言指定void*
类型的值与任何类型的对象指针兼容。添加强制转换可能会隐藏不包含声明calloc
的标头的错误。 在 C++ 中,规则有所不同。函数
calloc()
有两个参数:要分配的元素数量和每个元素的大小calloc
中,您尝试分配奇怪大小的wordCount
元素。我喜欢使用对象本身作为sizeof
运算符的操作数的 操作数- 在第二个
calloc
中,您尝试分配 50 个元素,每个元素的大小为 50。但您只需要每个wordCount
中有 1 个元素,对吧?另外,根据定义,sizeof (char)
是1
,因此它不会给您带来任何乘以它的东西。尝试这样
You don't need the casts for the return value of
calloc
. The C language specifies that a value of typevoid*
is compatible with any type of pointer to object. Adding the cast may hide the error of not including the header wherecalloc
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 onecalloc
you were trying to allocatewordCount
elements of a strange size. I like to use the object itself as operand to thesizeof
operatorcalloc
you were trying to allocate 50 elements of size 50 each. But you only want 1 element in eachwordCount
, right? Alsosizeof (char)
is, by definition,1
so it doesn't buy you anything to multiply by it.Try like this
尝试使用链表数据结构。
样本:
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.
在 sizeof() 中,您必须使用要分配的类型。指向 char 的指针与 char 本身是不同的类型,并且可能(并且在大多数情况下)具有不同的大小。例如:
另外,您不需要将指针的大小乘以字数,calloc 已经为您做到了这一点。你也可以这样做:
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:
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: