arrays - 在c中使用循环打印字符串的所有方法是什么
问题描述
char testArray[][50] = {"Mango", "Strawberry", "Bannana", "Cherry"};
for(int x = 0; x<testArray; x++){
printf("%s", testArray[x]);
}
我正在尝试找到可以使用 c 语言循环打印字符串的所有方法。任何帮助将非常感激。谢谢你。
解决方案
for 循环中的条件不正确。将整数与指针进行比较。
for(int x = 0; x<testArray; x++){
^^^^^^^^^^^
printf
由于使用了不正确的转换说明符来输出字符串,因此调用 调用未定义的行为。
printf("%c", testArray[x]);
^^^^
你可以写
char testArray[][50] = {"Mango", "Strawberry", "Bannana", "Cherry"};
const size_t N = sizeof( testArray ) / sizeof( *testArray );
for ( size_t i = 0; i < N; i++ )
{
printf( "%s\n", testArray[i] ); // or just puts( testArray[i] );
}
推荐阅读
- neural-network - 为什么神经网络损失函数总是正的
- list - 如何验证“页脚顶部”(Web 元素)背景图像颜色?
- java - 将时间戳中的句点 (AM/PM) 本地化为另一种语言
- c# - 有没有办法找到没有匹配的行?还是空?
- apache - 阿帕奇负载平衡
- android - 如何在片段android中访问全局变量
- r - 如何将指数回归修正为正偏态数据?
- azure - 将数据从 azure blob 存储传输到 hdfs 文件系统
- ruby-on-rails - 如何更改简单表单输入的标签
- powerbi - Power BI - 如何根据参数在 Power BI web 上获取报告