c - C - 将数组分配给指针时出现“不兼容的类型”警告
问题描述
我有两个单词列表。
我的代码随机选择一个列表,然后随机选择列表中的一个单词。
代码工作正常,但我收到incompatible pointer type
警告。
问题似乎与p = list1
.
但是,两者p
都有list1
一个 type char*
,所以我不明白这个警告。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void test() {
srand(time(NULL)); // seed the random number generator.
const char *list1[3] = { "one", "two", "three" }; // first list
int len1 = 3;
const char *list2[4] = { "uno", "dos", "tres", "quatro" }; // second list
int len2 = 4;
char **p; // variable to hold chosen list
int pcount; // size of the chosen list
char word[64] = "none"; // chosen word
int ran1 = rand() % 2; // random number 0 or 1
if (ran1 == 0) { p = list1; pcount = len1; } // warning: assignment from incompatible pointer type
if (ran1 == 1) { p = list2; pcount = len2; } // warning: assignment from incompatible pointer type
strcpy(word, p[rand() % pcount]);
printf("The word is %s.\n", word);
return;
}
解决方案
推荐阅读
- machine-learning - 使用差价时消失
- webrtc - STUN 如何对候选对执行 ICE 连接检查?
- sql - 使用 SQL Group By 或 Window Function 按时间顺序分析行?
- java - 比较2个字段的elasticsearch查询(使用java)
- javascript - 无需重新加载即可切换页面
- javascript - 带有已获取实例的 console.log() 在异步函数中停止执行
- javascript - 试图更改“total_price”变量的值,但在 Javascript 中得到“未定义”
- java - 如何比较 if 语句中的字符串?
- spring-boot - @valid @requestBody kotlin 与实体到实体
- bash - 如何从 bash 中变量的 curl 响应中获取字符串?