为什么在wprintf(L"\n请输入学生编号:"); scanf("%d", &a); form[a][0] = a; wprintf(L"请输入学生信息:\n"); wprintf(L"请输入学号:"); fgetws(form[a]+5, 9, stdin); wprintf(L"请输入姓名:"); fgetws(form[a]+9, 15, stdin); wprintf(L"请输入班级:"); fgetws(form[a]+15, 5, stdin); wprintf(L"请输入性别:"); fgetws(form[a]+19, 3, stdin); wprintf(L"请输入年龄:"); fgetws(form[a]+23, 5, stdin); wprintf(L"请输入宿舍:"); fgetws(form[a]+29, 4, stdin); wprintf(L"请输入电话号码:"); fgetws(form[a]+33, 11, stdin); wprintf(L"\n%s", form[0]); wprintf(L"\n%s", form[a]);中,wprintf(L"\n%s", form[0]); wprintf(L"\n%s", form[a]);会输出null
时间: 2024-04-23 13:27:17 浏览: 12
在执行wprintf(L"\n%s", form[0]);和wprintf(L"\n%s", form[a]);时,如果指向的字符串是空指针,那么就会输出null。这可能是因为在输入学生信息之前,没有对form数组进行初始化,导致其中的元素都是空指针。为避免这种情况,可以在使用指针前进行判空处理,或者在程序开始时对form数组进行初始化,例如使用memset函数将数组元素初始化为0。
相关问题
为什么以下代码不能输出中文printf("\n请输入学生编号:"); scanf("%d",&a); form[a][0]=a; printf("请输入学生信息:\n"); printf("请输入学号:"); scanf("%s",&form[a][5],&form[a][6],&form[a][7],&form[a][8]); printf("请输入姓名:"); scanf("%s",&form[a][9],&form[a][10],&form[a][11],&form[a][12],&form[a][13],&form[a][14]); printf("请输入班级:"); scanf("%s",&form[a][15],&form[a][16],&form[a][17],&form[a][18]); printf("请输入性别:"); scanf("%s",&form[a][19],&form[a][20],&form[a][21],&form[a][22]); printf("请输入年龄:"); scanf("%s",&form[a][23],&form[a][24],&form[a][25],&form[a][26],&form[a][27],&form[a][28]); printf("请输入宿舍:"); scanf("%s",&form[a][29],&form[a][30],&form[a][31],&form[a][32]); printf("请输入电话号码:"); scanf("%s",&form[a][33],&form[a][34],&form[a][35],&form[a][36],&form[a][37],&form[a][38],&form[a][39],&form[a][40],&form[a][41],&form[a][42],&form[a][43]); printf("\n%s",form[0][0],form[0][1],form[0][2],form[0][3],form[0][4],form[0][5],form[0][6],form[0][7],form[0][8],form[0][9],form[0][10],form[0][11],form[0][12],form[0][13],form[0][14],form[0][15],form[0][16],form[0][17],form[0][18],form[0][19],form[0][20],form[0][21],form[0][22],form[0][23],form[0][24],form[0][25],form[0][26],form[0][27],form[0][28],form[0][29],form[0][30],form[0][31],form[0][32],form[0][33],form[0][34],form[0][35],form[0][36],form[0][37],form[0][38],form[0][39],form[0][40],form[0][41],form[0][42],form[0][43]); printf("\n%s",form[a][0],form[a][1],form[a][2],form[a][3],form[a][4],form[a][5],form[a][6],form[a][7],form[a][8],form[a][9],form[a][10],form[a][11],form[a][12],form[a][13],form[a][14],form[a][15],form[a][16],form[a][17],form[a][18],form[a][19],form[a][20],form[a][21],form[a][22],form[a][23],form[a][24],form[a][25],form[a][26],form[a][27],form[a][28],form[a][29],form[a][30],form[a][31],form[a][32],form[a][33],form[a][34],form[a][35],form[a][36],form[a][37],form[a][38],form[a][39],form[a][40],form[a][41],form[a][42],form[a][43]); printf("\n1:继续录入 2:返回选择 3:关闭程序\n"); printf("请输入:"); scanf("%c",&o);
以上代码不能输出中文的原因是,使用了%s格式化字符串,但输入的却是字符数组,格式化字符串%s只能输出以'\0'结尾的字符数组,如果输入的字符数组没有以'\0'结尾,会导致输出乱码或者崩溃。
另外,scanf函数也存在问题,使用%s输入字符串时,只能输入不带空格的字符串,无法输入带空格的字符串。因此,需要使用其他函数如fgets等输入带空格的字符串。
正确的输出中文字符的方法是,使用宽字符函数wprintf输出,同时使用宽字符类型wchar_t来存储中文字符串,如:
```c
#include <stdio.h>
#include <wchar.h>
int main() {
int a;
wchar_t form[100][50];
wprintf(L"\n请输入学生编号:");
scanf("%d", &a);
form[a][0] = a;
wprintf(L"请输入学生信息:\n");
wprintf(L"请输入学号:");
fgetws(form[a]+5, 9, stdin);
wprintf(L"请输入姓名:");
fgetws(form[a]+9, 15, stdin);
wprintf(L"请输入班级:");
fgetws(form[a]+15, 5, stdin);
wprintf(L"请输入性别:");
fgetws(form[a]+19, 3, stdin);
wprintf(L"请输入年龄:");
fgetws(form[a]+23, 5, stdin);
wprintf(L"请输入宿舍:");
fgetws(form[a]+29, 4, stdin);
wprintf(L"请输入电话号码:");
fgetws(form[a]+33, 11, stdin);
wprintf(L"\n%s", form[0]);
wprintf(L"\n%s", form[a]);
wprintf(L"\n1:继续录入 2:返回选择 3:关闭程序\n");
wprintf(L"请输入:");
char o;
scanf("%c", &o);
return 0;
}
```
在上面的代码中,我们使用wchar_t类型来存储中文字符串,使用fgetws函数来输入带空格的字符串,使用wprintf函数来输出中文字符。需要注意的是,中文字符在wchar_t类型中占用两个字节,因此在输入和输出时需要指定读取或输出的字节数。
为什么wprintf(L"\n%s", form[0]); wprintf(L"\n%s", form[a]);会指向空指针
如果在执行wprintf(L"\n%s", form[0]);和wprintf(L"\n%s", form[a]);时,form[0]和form[a]指向了空指针,那么就会出现指针异常,导致程序崩溃或者输出不可预期的结果。可能的原因包括:未正确初始化指针,指针被释放或者指向了错误的内存地址等。为避免这种情况,可以在使用指针前进行判空处理,或者检查程序中指针的初始化和使用是否正确。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)