在C语言中写入char*数组会引发错误
我是一名信息安全专业的学生,刚刚开始学习C语言。我被指派使用这个原型编写一个简单的翻译程序:在C语言中写入char*数组会引发错误,c,arrays,pointers,C,Arrays,Pointers,我是一名信息安全专业的学生,刚刚开始学习C语言。我被指派使用这个原型编写一个简单的翻译程序:char*dictionary[numberofwords][2] 输出应如下所示: Enter # of words to add: 3 dictionary[0][0]= plane dictionary[0][1]= Flugzeug dictionary[1][0]= house dictionary[1][1]= Haus dictionary[2][0]= cat dictionary[2]
char*dictionary[numberofwords][2]代码>
输出应如下所示:
Enter # of words to add: 3
dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
dictionary[2][0]= cat
dictionary[2][1]= Katze
Enter english word to translate: house
---> Haus
void clean_stdin(void);
int main(void)
{
unsigned int i = 0,j = 0,size;
printf("Enter # of words to add: ");
scanf("%u",&size);
clean_stdin(); //clears input buffer
char* dictionary[size][2];
while(i <= size)
{
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],100,stdin);
if(j >= 1)
{
j = 0;
++i;
}
else
j = 1;
}
return 0;
}
我当前的(正在进行的工作)代码如下所示:
Enter # of words to add: 3
dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
dictionary[2][0]= cat
dictionary[2][1]= Katze
Enter english word to translate: house
---> Haus
void clean_stdin(void);
int main(void)
{
unsigned int i = 0,j = 0,size;
printf("Enter # of words to add: ");
scanf("%u",&size);
clean_stdin(); //clears input buffer
char* dictionary[size][2];
while(i <= size)
{
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],100,stdin);
if(j >= 1)
{
j = 0;
++i;
}
else
j = 1;
}
return 0;
}
我的思维过程中有一个根本性的缺陷。非常感谢您的帮助/提醒。干杯 fgets(字典[i][j],100,标准文本)
读取未初始化的变量(字典[i][j]
从未给定值),该变量具有未定义的行为
另外,i
稍后也会越界。让我们看看代码的这一部分:
char* dictionary[size][2];
while(i <= size)
{
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],100,stdin);
或者,您需要为每个数组项动态分配内存:
while ( i < size ) // <, not <=
{
dictionary[i][j] = malloc( sizeof *dictionary[i][j] * (MAX_STR_LEN + 1));
if ( !dictionary[i][j] )
{
// memory allocation failed, handle error
}
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],MAX_STR_LEN,stdin);
我建议您使用以下结构,而不是混淆多维数组:
#include <stdio.h>
#include <string.h>
int main()
{
unsigned int size;
printf("Enter # of words to add: ");
if (scanf("%u", &size) != 1)
return 1; // cannot convert input to integer
#define WORD_MAX_SIZE 100
struct {
char word[WORD_MAX_SIZE];
char translation[WORD_MAX_SIZE];
} dictionary[size];
memset(dictionary, 0, sizeof(dictionary));
for (int i = 0; i < size; i++) {
printf("dictionary[%u].word= ", i);
fgets(dictionary[i].word, WORD_MAX_SIZE, stdin);
printf("dictionary[%u].translation= ", i);
fgets(dictionary[i].translation, WORD_MAX_SIZE, stdin);
}
// do something with your stuff
}
#包括
#包括
int main()
{
无符号整数大小;
printf(“输入要添加的单词:”);
如果(扫描频率(“%u”,&size)!=1)
返回1;//无法将输入转换为整数
#定义单词\u最大\u大小100
结构{
字符字[字的最大大小];
字符翻译[单词最大大小];
}字典[大小];
memset(dictionary,0,sizeof(dictionary));
对于(int i=0;i
实际上fgets并没有从字典中读取数据,它会写入字典指向的内存,这更糟糕。@DaleWilson我不是说fgets正在读取数据。我是说表达式从变量中读取是为了得到传递给函数的值。请显示一个。也许是学习如何使用调试器的时候了。内存在哪里分配给字典元素应该指向的内存?dictionary
是size
array[2
]指向char的指针数。指针是一个变量,它将其他对象的地址存储为其值。当您声明一个指针而不初始化它以指向其他对象时,它是一个未初始化的指针。(它不存储任何其他内容的地址)。就像inta
声明一个不带值的a
。指针指向某物的地址(即指向内存地址)。您可以通过将有效内存块的地址从现有的或新分配的内存中分配给指针来提供该地址。我不允许使用结构。在这种情况下,将此练习视为您将来不想编写的代码的例证。(IMHO,构建代码总比用三维数组搔头好…)然而,dura lex sed lex。
#include <stdio.h>
#include <string.h>
int main()
{
unsigned int size;
printf("Enter # of words to add: ");
if (scanf("%u", &size) != 1)
return 1; // cannot convert input to integer
#define WORD_MAX_SIZE 100
struct {
char word[WORD_MAX_SIZE];
char translation[WORD_MAX_SIZE];
} dictionary[size];
memset(dictionary, 0, sizeof(dictionary));
for (int i = 0; i < size; i++) {
printf("dictionary[%u].word= ", i);
fgets(dictionary[i].word, WORD_MAX_SIZE, stdin);
printf("dictionary[%u].translation= ", i);
fgets(dictionary[i].translation, WORD_MAX_SIZE, stdin);
}
// do something with your stuff
}