#include<stdio.h> #include<string.h> int lth(char str[][], int size){ int count = 0; while(count < size && str[count][0] != NULL){ count++; } return count; } int main(){ FILE *f; char temp[100], str[20][100]; int i = 0; f = fopen("./student.txt", "r"); while(fgets(temp, sizeof(temp), f) != NULL && i < 20){ int t = 0; while(temp[t] != '\n' /*&& temp[t] != '\0'*/){ t++; } temp[t] = NULL; printf("%s\n", temp); strncpy(str[i], temp, sizeof(str[i])); printf("%s\n", str[i]); i++; } printf("\n"); int j; j = lth(str, 20); printf("%d\n", j); return 0; }这段代码有什么错误
时间: 2024-04-27 15:21:16 浏览: 69
这段代码的错误在于函数定义时的参数类型不正确,应该是 `char str[][100]` 而不是 `char str[][]`。另外,在将字符串复制到二维字符数组 `str` 中时,使用了 `strncpy` 函数,但没有为字符串添加结束符。应该在复制完成后添加 `str[i][t] = '\0'` 来保证字符串的正确性。
相关问题
#include <stdio.h> #include <stdlib.h> void PutRes(void); int fun(char *); int fun (char *p) 接着这个编写C语言:通过函数统计字符串中数字字符的个数,返回结果。在主函数中输入一串字符
#include <stdio.h>
#include <stdlib.h>
int count_digits(char*);
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
int count = count_digits(str);
printf("Number of digits in the string: %d\n", count);
return 0;
}
int count_digits(char *str) {
int count = 0;
while (*str != '\0') {
if (*str >= '0' && *str <= '9') {
count++;
}
str++;
}
return count;
}
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![h](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)