使用指针数组的拼写检查程序
Spell checking program using pointer arrays
我正在尝试创建一个拼写检查程序,该程序读取包含 ~3000 个无序的 3-4 个字母单词的文件,每个单词在一行上,将它们按字母顺序排序,然后打印出来。
我有一个 working version 使用“标准”数组形式 array[][],但是我正在尝试修改程序以仅使用指针。我认为通过根据 char * 的大小分配数组我的文件的大小会有足够的内存让程序正确执行,但是当我 运行 我的代码时,我一直得到 SEGV,指向我的 while循环。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bubbleSortWordsArray(char **array, int wordCount, int arrSize);
void printWordsArray(char **array, int wordCount);
int numOfLines(FILE *filePoint);
int main(int argc, char **argv) {
FILE *fp = fopen(argv[1], "r");
int i = 0, size = numOfLines(fp);
char **words = (char **)malloc(sizeof(char) * size);
if (fp == NULL) {
fprintf(stderr, "fopen failed");
exit(EXIT_FAILURE);
}
while (fgets(words[i], size, fp)) {
words[i][strlen(words[i]) - 1] = '[=10=]';
i++;
}
fclose(fp);
bubbleSortWordsArray(words, i, size);
printWordsArray(words, i);
free(words);
return (0);
}
void bubbleSortWordsArray(char **array, int wordCount, int arrSize)
{
int c;
int d;
char *swap = (char *)malloc(sizeof(char) * arrSize);
for (c = 0; c < (wordCount - 1); c++) {
for (d = 0; d < (wordCount - c - 1); d++) {
if (0 > strcmp(array[d], array[d + 1])) {
strcpy(swap, array[d]);
strcpy(array[d], array[d + 1]);
strcpy(array[d + 1], swap);
}
}
}
}
void printWordsArray(char **array, int wordCount)
{
int i;
printf("\n");
for (i = 0; i < wordCount; i++) {
printf("%s\n", array[i]);
}
}
int numOfLines(FILE *filePoint) {
int c, count;
count = 0;
for (;; ) {
c = fgetc(filePoint);
if (c == EOF)
break;
if (c == '\n')
++count;
}
rewind(filePoint);
return count+1;
}
尝试这样的事情:
char **words = (char **)malloc(sizeof(char*) * size);
for(i = 0; i < size; i++){
words[i] = (char*)malloc(sizeof(char) * (someValue + 2));
}
其中 someValue 将是您单词的最大长度(如果您只有 3-4 个字母的单词,则将其设置为 4)。向此 someValue 添加 2,这样您还可以存储 '\n' 和 '\0' 信号每个单词的结尾。每个单词都将是一个新字符串。
char **words = (char **)malloc(sizeof(char) * size);
首先,跳过转换并使用变量而不是类型。这样您就可以避免使用 char
而不是 char*
造成的错误。在这里阅读更多相关信息:
char **words = malloc(sizeof(*words) * size);
其次。您在这里所做的只是为一些指针分配 space,但这些指针并不指向任何地方。之后你需要这样的东西:
for(int i=0; i<size; i++)
words[i] = malloc(sizeof (*words[0]) * maxwordsize);
其中 maxwordsize
需要在某处定义。每个指针可能会有所不同。
我正在尝试创建一个拼写检查程序,该程序读取包含 ~3000 个无序的 3-4 个字母单词的文件,每个单词在一行上,将它们按字母顺序排序,然后打印出来。
我有一个 working version 使用“标准”数组形式 array[][],但是我正在尝试修改程序以仅使用指针。我认为通过根据 char * 的大小分配数组我的文件的大小会有足够的内存让程序正确执行,但是当我 运行 我的代码时,我一直得到 SEGV,指向我的 while循环。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bubbleSortWordsArray(char **array, int wordCount, int arrSize);
void printWordsArray(char **array, int wordCount);
int numOfLines(FILE *filePoint);
int main(int argc, char **argv) {
FILE *fp = fopen(argv[1], "r");
int i = 0, size = numOfLines(fp);
char **words = (char **)malloc(sizeof(char) * size);
if (fp == NULL) {
fprintf(stderr, "fopen failed");
exit(EXIT_FAILURE);
}
while (fgets(words[i], size, fp)) {
words[i][strlen(words[i]) - 1] = '[=10=]';
i++;
}
fclose(fp);
bubbleSortWordsArray(words, i, size);
printWordsArray(words, i);
free(words);
return (0);
}
void bubbleSortWordsArray(char **array, int wordCount, int arrSize)
{
int c;
int d;
char *swap = (char *)malloc(sizeof(char) * arrSize);
for (c = 0; c < (wordCount - 1); c++) {
for (d = 0; d < (wordCount - c - 1); d++) {
if (0 > strcmp(array[d], array[d + 1])) {
strcpy(swap, array[d]);
strcpy(array[d], array[d + 1]);
strcpy(array[d + 1], swap);
}
}
}
}
void printWordsArray(char **array, int wordCount)
{
int i;
printf("\n");
for (i = 0; i < wordCount; i++) {
printf("%s\n", array[i]);
}
}
int numOfLines(FILE *filePoint) {
int c, count;
count = 0;
for (;; ) {
c = fgetc(filePoint);
if (c == EOF)
break;
if (c == '\n')
++count;
}
rewind(filePoint);
return count+1;
}
尝试这样的事情:
char **words = (char **)malloc(sizeof(char*) * size);
for(i = 0; i < size; i++){
words[i] = (char*)malloc(sizeof(char) * (someValue + 2));
}
其中 someValue 将是您单词的最大长度(如果您只有 3-4 个字母的单词,则将其设置为 4)。向此 someValue 添加 2,这样您还可以存储 '\n' 和 '\0' 信号每个单词的结尾。每个单词都将是一个新字符串。
char **words = (char **)malloc(sizeof(char) * size);
首先,跳过转换并使用变量而不是类型。这样您就可以避免使用 char
而不是 char*
造成的错误。在这里阅读更多相关信息:
char **words = malloc(sizeof(*words) * size);
其次。您在这里所做的只是为一些指针分配 space,但这些指针并不指向任何地方。之后你需要这样的东西:
for(int i=0; i<size; i++)
words[i] = malloc(sizeof (*words[0]) * maxwordsize);
其中 maxwordsize
需要在某处定义。每个指针可能会有所不同。