在字符串数组兼容的指针类型的警告 [英] Incompatible pointer type warnings in array of strings
问题描述
我想出C.字符串数组我有一个字符串数组字典,我补充的话,然后打印出数组,看看它的工作。输出的作品,因为我认为它应该打印在阵列中的话。但我得到一个数字,我无法修复警告。
I am trying out arrays of strings in C. I have a dictionary array of strings, that I add words to and then print out the array to see if it worked. The output works, as i think it should, printing the words in the array. But I get a number of warnings that I am unable to fix.
// 20 word dictionary
#define ROWS 20
#define WORD_LENGTH 10
char dictionary[ROWS][WORD_LENGTH];
void add_word(char **dict, int index, char *word) {
dict[index] = word;
}
char *get_word(char **dict, int index) {
return dict[index];
}
void print_dictionary(char **dict) {
int i;
for (i = 0; i < 20; i++) {
printf("%d: %s\n", i, get_word(dict, i));
}
}
void test_dictionary() {
add_word(dictionary, 0, "lorem");
add_word(dictionary, 1, "ipsum");
print_dictionary(dictionary);
}
int main() {
test_dictionary();
}
编译此的输出是,
The output of compiling this is,
p5.c: In function ‘test_dictionary’:
p5.c:54:2: warning: passing argument 1 of ‘add_word’ from incompatible pointer type [enabled by default]
p5.c:38:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
p5.c:55:2: warning: passing argument 1 of ‘add_word’ from incompatible pointer type [enabled by default]
p5.c:38:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
p5.c:57:2: warning: passing argument 1 of ‘print_dictionary’ from incompatible pointer type [enabled by default]
p5.c:46:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
我试图改变**字典与dict [行数] [WORD_LENGTH]没有太大的差别。你们能否请您解释如何声明这本词典的参数。谢谢你。
I tried changing **dict to dict[ROWS][WORD_LENGTH] without much difference. Can you guys please explain how to declare this dictionary parameter. Thanks.
编辑:我的编译器标志,CFLAGS = -Wall -g
My compiler flags are, CFLAGS = -Wall -g.
EDIT2:改变了报关单,
Changed the declarations to,
void add_word(char dict[][WORD_LENGTH], int index, char *word) {
dict[index] = word;
}
char *get_word(char dict[][WORD_LENGTH], int index) {
return dict[index];
}
void print_dictionary(char dict[][WORD_LENGTH]) {
int i;
for (i = 0; i < 20; i++) {
printf("%d: %s\n", i, get_word(dict, i));
}
}
这给出了一个编译错误,
This gives a compilation error,
p5.c: In function ‘add_word’:
p5.c:42:14: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
make[1]: *** [p5] Error 1
感谢您的一切帮助。
啊!弄清楚了!。因为它是一个指针,我需要使用的strcpy由@Jack的建议。
Ah! Figured it out!. Since it's a pointer, I need to use strcpy as suggested by @Jack.
void add_word(char dict[][WORD_LENGTH], int index, char *word) {
/*dict[index] = word;*/
strcpy(dict[index], word);
}
谢谢大家!
推荐答案
根本原因:
数组不是指针!
请注意,有没有从隐式转换字典[] []
到字典**
,因此错误。当你传递一个阵列功能,它衰变为指针二维数组的第一element.In情况下,第一个元素是单dimenion本身的数组,所以你需要一个指向数组的指针,而不是一个双指针。
Note that there is no implicit conversion from dict[][]
to dict **
and hence the error. When you pass a array to a function, it decays as the pointer to its first element.In case of two dimensional array the first element is an array of single dimenion itself and so you need a pointer to an array and not an double pointer.
Soultion:
您需要修改你的函数原型匹配的键入的你逝去。
You need to modify your function prototype to match the type you are passing.
void add_word(char dict[][WORD_LENGTH], int index, char *word);
void print_dictionary(char dict[][WORD_LENGTH]);
这篇关于在字符串数组兼容的指针类型的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!