简体   繁体   English

C语言中的多维字符串数组

[英]Multi dimensional string arrays in C

I'm just doing some extra work on my own to try and get a better grasp of multi dimensional string arrays in C, for example array[3][5]= {"apple","house","truck"}. 我只是自己做一些额外的工作,试图更好地理解C中的多维字符串数组,例如array [3] [5] = {“ apple”,“ house”,“ truck”}。 I have a test file filled with many words with varying length, and want to fill the string array with these different words.I've used dynamic allocation to provide memory space, open the file, and the use fgets to get each word off because each word is on a new line. 我有一个测试文件,其中充满了许多长度不一的单词,并想用这些不同的单词来填充字符串数组。我使用了动态分配来提供内存空间,打开文件并使用fgets删除每个单词,因为每个单词都换一行。 I save the word into a new place in the array, and then print it to check if it has saved. 我将单词保存到数组中的新位置,然后将其打印以检查它是否已保存。 The words print like they should, which makes me believe that they are being stored, but then i get a seg fault. 这些单词的打印方式应该像它们应该的那样,这使我相信它们已被存储,但是随后出现段错误。 Can anyone explain to me why this is happening? 谁能向我解释为什么会这样?

A sample of the text file and the form I have it in is(without the blank lines between words: 文本文件及其形式的示例是(单词之间没有空白行:

enchantment 魅力

enchantress 女巫

enchants 附魔

misusing 滥用

Mitch 米奇

Mitchell 米切尔

miter 斜接

mitigate 减轻

mitigated 减轻

mitigates 减轻

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

#define WORDS 50
#define LETTERS 15

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

int i;
char **array;
FILE *file1;
char string[15];

array=(char **)malloc(LETTERS*sizeof(char*));

for (i=0;i<WORDS;i++) {
    array[i]=(char *)malloc(LETTERS*sizeof(char));
}

if (argc != 2) {
    printf("\nERROR: Wrong number of arguments entered\n");
    return -1;
}

file1=fopen(argv[1],"r");

if (file1==NULL) {

    printf("\nERROR: File 1 not found\n");
    return -1;
}

for (i=0;i<=WORDS;i++) {
    fgets(string,LETTERS,file1);
    array[i][0]=*string;
    printf("%s",string);
}

return 0;

}

从您的示例中,您需要为每个字符串至少分配6个字符,否则将删除终端的null字符。

Dynamic memory allocation were wrong in your code. 动态内存分配在您的代码中是错误的。

Instead of this code array=(char **)malloc(LETTERS*sizeof(char*)); 代替此代码array=(char **)malloc(LETTERS*sizeof(char*)); replace the following code 替换以下代码

array=(char **)malloc(WORDS*sizeof(char *));
for(i=0;i<WORDS;i++)
array[i]=(char *)malloc(LETTERS*sizeof(char));

Reading the data from the file also you need to modify. 从文件读取数据也需要修改。

Instead of this code for (i=0;i<=WORDS;i++) { fgets(string,LETTERS,file1); array[i][0]=*string; printf("%s",string); } 代替此代码for (i=0;i<=WORDS;i++) { fgets(string,LETTERS,file1); array[i][0]=*string; printf("%s",string); } for (i=0;i<=WORDS;i++) { fgets(string,LETTERS,file1); array[i][0]=*string; printf("%s",string); }

replace the following code 替换以下代码

i=0;
while(fgets(string,LETTERS,file1)!=NULL){
strcpy(array[i],string);
printf("%s",string);
i++;
}

Now i holds the value of total string read from the file. 现在i拥有从文件中读取的总字符串的值。 For printing the content of array 用于打印数组的内容

int j;
for(j=0;j<i;j++)
printf("%s",array[j]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM