arrays - 是否可以像创建整数数组一样创建字符串数组?
问题描述
我正在尝试通过执行以下操作来创建一个字符串数组:
#include <stdio.h>
#include <cs50.h>
string words[] = {apple, bear, card, duck};
int main(void)
{
for(int i = 0; i < 4; i++)
{
printf("%s", words[i]);
}
}
我认为这是创建数组的方法之一,但是当我尝试编译时,我得到error: use of undeclared identifier
了apple
,和. 但是,当我用整数尝试同样的事情时:bear
card
duck
#include <cs50.h>
#include <stdio.h>
int numbers[] = {1, 2, 3, 4};
int main(void)
{
for(int i = 0; i < 4; i++)
{
printf("%i", numbers[i]);
}
}
它可以顺利编译和运行。使用这种方法根本不可能创建一个字符串数组,还是我错过了其他东西?任何帮助将非常感激。
解决方案
字符串必须用 引用"
。
#include <stdio.h>
#include <cs50.h>
const char *words[] = {"apple", "bear", "card", "duck"};
int main(void)
{
for(int i = 0; i < 4; i++)
{
printf("%s", words[i]);
}
}
推荐阅读
- express - 摩根与 Express 不记录静态文件
- c# - 限界上下文的物化是什么?(C#/.NET)
- c# - 参数对sql命令不生效
- json - 如何反序列化具有重复键的 JSON 而不会丢失任何值?
- python - 多元时间序列的正确 LSTM 输入形状是什么?
- android - Jetpack Navigation:在 Fragment 之间导航时,如何使用与启动新 Activity 时相同的动画?
- python - 如何为多对多查询返回完全匹配
- c++ - 在 C++17 中以函数方式进行笛卡尔积
- javascript - React 部署到 gh-pages 时不显示放置在公用文件夹中的图像,但在本地主机上工作,为什么?
- android - Android ndk jni 问题与 GetMethodID GetStaticMethodID