gpt4 book ai didi

c - 程序不输出存储在数组中的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:07 24 4
gpt4 key购买 nike

我正在编写一个程序来显示姓名和公寓数量,但是,我存储姓名的数组无法显示姓名,表示他们身份不明。无论如何要显示数组中包含的字符串?另外,我似乎在显示的公寓数量下方显示了 n 的值,是否有办法摆脱这个?这是我的代码:

#include <stdio.h>

int main(void)
{
int i;
char name[] = {North, West, South, East};
int apt[] = {24, 30, 14, 18};
const int n = 5;

printf("Name No. of Apartments\n");
for (i = 0; i < n; i++)
printf("%c %d\n", name[i], apt[i]);

return 0;

}

最佳答案

这是您的代码,已更正:

#include <stdio.h>

int main(void)
{
int i;
char *name[] = {"North", "West", "South", "East"}; /* You're declaring an array of characters, you need an array of strings/pointers */
int apt[] = {24, 30, 14, 18};
const int n = 4; /* you have 4 elements in your array, not 5 */

printf("Name No. of Apartments\n");
for (i = 0; i < n; i++)
printf("%s %d\n", name[i], apt[i]); /* %c is for characters, you need %s for strings */

return 0;
}

关于c - 程序不输出存储在数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42283228/

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