gpt4 book ai didi

c - C语言中如何计算数组的元素个数?

转载 作者:行者123 更新时间:2023-11-30 15:45:29 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>
int main()
{
char name[32][32];
char input[32];
int number;
int i;

for(i=0;i<10;i++)
{
fgets(input,sizeof(input),stdin);
sscanf(input,%s,name[i]);

}

//assume that we don't know variable name have 10 element of arrays.

//function to count how many elements of arrays to stored at number.

for(i=0;i<number;i++)
{
printf("%s",name[i]);
}
}

什么函数可以计算数组的元素?

最佳答案

将目标字符串初始化为 0,然后在打印时检查它们是否不为空怎么样?

#include <stdio.h>
#include <string.h>
int main()
{
char name[32][32] = {0};
...
for(i=0;i<32;i++)
{
if(name[i][0]!='\0')
printf("%s",name[i]);
}
}

关于c - C语言中如何计算数组的元素个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19026687/

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