请指出下面代码中的错误 static char *student[MAX_STUDENT_NUM] = (NULL); int find_student_id_by_name (const char *name) { int i = 0; for (i = 0; student[i] != NULL && i < MAX_STUDENT_NUM; ++i){ if (0 == strcmp(student[i], name)) return i; } return -ERR_NOT_FIND; }
时间: 2024-01-31 15:02:52 浏览: 27
下面是代码的错误和修改建议:
错误:
1. 在静态变量定义时不能使用赋值符号"="。
2. `MAX_STUDENT_NUM`没有定义。
3. `ERR_NOT_FIND`没有定义。
4. `strcmp`函数需要包含头文件`string.h`。
修改建议:
1. 静态变量定义只需要指定类型和变量名,不需要使用赋值符号"="。
2. 定义`MAX_STUDENT_NUM`常量,保证数组不越界。
3. 定义`ERR_NOT_FIND`常量,作为返回错误的标志。
4. 在函数中使用`strcmp`函数前先包含头文件`string.h`。
修改后的代码如下:
```
#include <string.h>
#define MAX_STUDENT_NUM 100
#define ERR_NOT_FIND -1
static char *student[MAX_STUDENT_NUM];
int find_student_id_by_name (const char *name) {
int i = 0;
for (i = 0; i < MAX_STUDENT_NUM && student[i] != NULL; ++i) {
if (0 == strcmp(student[i], name))
return i;
}
return ERR_NOT_FIND;
}
```
相关问题
8.请指出下面代码中的错误 static char *student[MAX_STUDENT_NUM] = (NULL); int find_student_id_by_name (const char *name) { int i = 0; for (i = 0; student[i] != NULL && i < MAX_STUDENT_NUM; ++i){ if (0 == strcmp(student[i], name)) return i; } return -ERR_NOT_FIND; }
下面是代码错误的解释和修正建议:
1. static char *student[MAX_STUDENT_NUM] = (NULL);
这行代码中,使用了括号将 NULL 括起来,这是错误的。正确的写法是:
static char *student[MAX_STUDENT_NUM] = {NULL};
2. 在函数 find_student_id_by_name 中,变量 ERR_NOT_FIND 没有定义,应该先定义它。
3. 在比较字符串时,应该使用 strcmp 函数,而不是 == 进行比较。
下面是修正后的代码:
#define ERR_NOT_FIND -1
static char *student[MAX_STUDENT_NUM] = {NULL};
int find_student_id_by_name(const char *name) {
int i = 0;
for (i = 0; student[i] != NULL && i < MAX_STUDENT_NUM; ++i) {
if (0 == strcmp(student[i], name)) {
return i;
}
}
return ERR_NOT_FIND;
}
8.请指出下面代码中的错误 static char *student[MAX_STUDENT_NUM] = {NULL}; int find_student_id_by_name (const char *name) { int i = 0; for (i = 0; student[i] != NULL && i < MAX_STUDENT_NUM; ++i){ if (0 == strcmp(student[i], name)) return i; } return -ERR_NOT_FIND; }
代码中存在两个错误:
1. `student[i]` 是一个 `char*` 类型的指针,应该与 `name` 进行比较,而不是与 `0` 进行比较。
2. 在比较 `student[i]` 和 `name` 之前,应该先判断 `student[i]` 是否为 NULL,否则可能会出现空指针异常。
下面是修改后的代码:
```
static char *student[MAX_STUDENT_NUM] = {NULL};
int find_student_id_by_name (const char *name) {
int i = 0;
for (i = 0; i < MAX_STUDENT_NUM && student[i] != NULL; ++i) {
if (strcmp(student[i], name) == 0)
return i;
}
return -ERR_NOT_FIND;
}
```
其中,`strcmp` 函数用于比较两个字符串是否相等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)