gpt4 book ai didi

c - 打印多维字符数组

转载 作者:行者123 更新时间:2023-11-30 20:44:47 24 4
gpt4 key购买 nike

我正在尝试在 C 中打印以下二维字符串数组,如下所示:

char text[10][80] = {
"0", "Zero",
"1", "One",
"2", "Two",
"3", "Three",
"4", "Four",
};

输出应该是这样的:

0 Zero
1 One
2 Two
3 Three
4 Four

我编写了以下程序:

#include <stdio.h>

int main()
{
char text[10][80] = {
"0", "Zero",
"1", "One",
"2", "Two",
"3", "Three",
"4", "Four",
};
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<1; j++)
{
printf("%s ", text[i]);
}
}
return 0;
}

它没有为我提供所需的输出。我尝试了几种方法,但没有运气。

最佳答案

您可以修改循环以获得所需的输出 -

for(i=0; i<9; i=i+2){
printf("%s %s\n", text[i], text[i+1]);
}

此循环将以所需格式打印索引为 ii+1 的数组内容。例如索引 0123 处的值,依此类推。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com