c - 打印出数组中的字母

标签 c

我尝试了很多方法,但找不到任何解决问题的方法。我试图一一打印数组的内容,但它崩溃了。如何让每次迭代后打印出每个索引对应的字母?

main() {
   int i;
   char myArray[10];

   for(i = 0; i < 5; i++) {
    myArray[i] = "a";
    printf("%s\n", myArray[i]);
   }

}

我也尝试过:

printf("%c\n", myArray[i])

和:

printf("%d\n", (int)myArray[i]) gives me the numbers 36.

最佳答案

  1. 您的 main 函数没有返回值。您需要使用 int 或 void。

  2. arr[i] = "a";将空终止字符串分配给 arr[i] ,您想要分配一个字符。

  3. 什么是arr ?您的阵列的名称是 myArray !

  4. printf("%s\n", myArray[i]);将解释myArray[i]作为字符串,但它是一个字符。

这是更正后的程序:

int main() 
{
    int i;
    char myArray[10];

    for (i = 0; i < 5; i++) 
    {
        myArray[i] = 'a';
        printf("%c\n", myArray[i]);
    }
}

关于c - 打印出数组中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954835/

相关文章:

c++ - 有没有一个gcc命令可以链接同一目录下的所有.o文件并生成一个.exe文件?

使用 libCURL 在 C 中创建 Azure Blob 存储

c - 指向 3D 数组数据切片的 2D 指针数组

c - 为什么我设置xlib窗口背景透明失败?

c - 什么样的企业仍然雇用 C 程序员?

c - 需要 strlen 帮助

c - 数字大于 2³² 的按位取模

c - 在头文件中调用枚举时遇到错误

c - ".global"的宏定义

c - Malloc、free 和段错误