新手求助,c语言学习s1e38动动手的最后一题
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int main()
{
printf("============================\n");
printf("* 欢迎使用此程序,指令如下 *\n");
printf("* 1.生成一个 M*N 的矩阵 *\n");
printf("* 2.初始化矩阵 *\n");
printf("* 3.给矩阵中某个元素赋值 *\n");
printf("* 4.读取矩阵中的某个元素 *\n");
printf("* 5.打印整个矩阵 *\n");
printf("* 6.结束程序 *\n");
printf("============================\n");
int order = 0, indicator = 0, data;
int M, N, m, n, i, j;
//M,N用来表示矩阵规模
//m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
//data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
//i,j用来作为输出二维数组的标志
char judge;
while(order != 6)
{
printf("请输入指令:");
scanf("%d", &order);
switch(order)
{
case 1:
if(indicator)
{
printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
scanf("%c", &judge);
if(judge == 'N')
{
continue;
}
}
printf("请输入新矩阵的规模(M*N)-> ");
scanf("%d*%d", &M, &N);
int (*pstr);
pstr = (int (*))calloc(M * N, sizeof(int));
if(pstr == NULL)
{
printf("内存空间不足!\n");
exit(1);
}
printf("%d*%d的新矩阵创建成功!\n", M, N);
indicator = 1;
break;
case 2:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入一个数字:");
scanf("%d", &data);
memset(pstr, data, M * N * sizeof(int));
break;
case 3:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要修改的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("请输入一个数字:");
scanf("%d", &data);
pstr = data;
break;
case 4:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要读取的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("第%d行,第%d列的数字是:%d\n", m, n, pstr);
break;
case 5:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
for(i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
{
printf("%4d", pstr);
}
printf("\n");
}
break;
case 6:
printf("感谢使用本程序^_^\n");
break;
default:
printf("当前输入的指令无效,请重新输入!\n");
continue;
}
}
free(pstr);
return 0;
}
为什么编译的时候会如下报错?
||=== 构建: Debug 在 test2 中 (编译器: GNU GCC Compiler) ===|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c||In function 'main':|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|56|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|56|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|68|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|68|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|83|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|83|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|94|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|94|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|110|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|110|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|113|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|113|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|119|error: 'pstr' undeclared (first use in this function)|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|119|note: each undeclared identifier is reported only once for each function it appears in|
||=== 构建 失败: 13 error(s), 0 warning(s) (0 分, 0 秒) ===|
自己看吧。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int main()
{
printf("============================\n");
printf("* 欢迎使用此程序,指令如下 *\n");
printf("* 1.生成一个 M*N 的矩阵 *\n");
printf("* 2.初始化矩阵 *\n");
printf("* 3.给矩阵中某个元素赋值 *\n");
printf("* 4.读取矩阵中的某个元素 *\n");
printf("* 5.打印整个矩阵 *\n");
printf("* 6.结束程序 *\n");
printf("============================\n");
int order = 0, indicator = 0, data;
int M, N, m, n, i, j;
//M,N用来表示矩阵规模
//m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
//data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
//i,j用来作为输出二维数组的标志
char judge;
int (*pstr);
pstr = (int (*))calloc(M * N, sizeof(int));
while(order != 6)
{
printf("请输入指令:");
scanf("%d", &order);
switch(order)
{
case 1:
{
if(indicator)
{
printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
scanf("%c", &judge);
if(judge == 'N')
{
continue;
}
}
printf("请输入新矩阵的规模(M*N)-> ");
scanf("%d*%d", &M, &N);
if(pstr == NULL)
{
printf("内存空间不足!\n");
exit(1);
}
printf("%d*%d的新矩阵创建成功!\n", M, N);
indicator = 1;
break;
}
case 2:
{
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入一个数字:");
scanf("%d", &data);
memset(pstr, data, M * N * sizeof(int));
break;
}
case 3:
{
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要修改的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("请输入一个数字:");
scanf("%d", &data);
pstr = data;
break;
}
case 4:
{
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要读取的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("第%d行,第%d列的数字是:%d\n", m, n, pstr);
break;
}
case 5:
{
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
for(i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
{
printf("%4d", pstr);
}
printf("\n");
}
break;
}
case 6:
{
printf("感谢使用本程序^_^\n");
break;
}
default:
{
printf("当前输入的指令无效,请重新输入!\n");
continue;
}
}
}
free(pstr);
return 0;
}
为啥你是新手 ba21 发表于 2022-4-9 23:29
自己看吧。
可是输出的时候结果是内存不足
然后我把代码改成如下又变成新的问题了(没有再输出内存不足)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int main()
{
printf("============================\n");
printf("* 欢迎使用此程序,指令如下 *\n");
printf("* 1.生成一个 M*N 的矩阵 *\n");
printf("* 2.初始化矩阵 *\n");
printf("* 3.给矩阵中某个元素赋值 *\n");
printf("* 4.读取矩阵中的某个元素 *\n");
printf("* 5.打印整个矩阵 *\n");
printf("* 6.结束程序 *\n");
printf("============================\n");
int order = 0, indicator = 0, data;
int M, N, m, n, i, j;
//M,N用来表示矩阵规模
//m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
//data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
//i,j用来作为输出二维数组的标志
char judge;
int (*pstr);
//pstr = (int (*))calloc(M * N, sizeof(int));
while(order != 6)
{
printf("请输入指令:");
scanf("%d", &order);
switch(order)
{
case 1:
if(indicator)
{
printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
scanf("%c", &judge);
if(judge == 'N')
{
continue;
}
}
printf("请输入新矩阵的规模(M*N)-> ");
scanf("%d*%d", &M, &N);
pstr = (int (*))calloc(M * N, sizeof(int));
if(pstr == NULL)
{
printf("内存空间不足!\n");
exit(1);
}
printf("%d*%d的新矩阵创建成功!\n", M, N);
indicator = 1;
break;
case 2:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入一个数字:");
scanf("%d", &data);
memset(pstr, data, M * N * sizeof(int));
break;
case 3:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
while(1)
{
printf("请输入要修改的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
if(m > M || n > N)
{
printf("您输入的位置不在矩阵中,请重新输入!\n");
}
else
{
break;
}
}
//以上用来判断输入的位置是否超过矩阵范围
printf("请输入一个数字:");
scanf("%d", &data);
pstr = data;
break;
case 4:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
while(1)
{
printf("请输入要读取的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
if(m > M || n > N)
{
printf("您输入的位置不在矩阵中,请重新输入!\n");
}
else
{
break;
}
}
//以上用来判断输入的位置是否超过矩阵范围
printf("第%d行,第%d列的数字是:%d\n", m, n, pstr);
break;
case 5:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
for(i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
{
printf("%4d", pstr);
}
printf("\n");
}
break;
case 6:
printf("感谢使用本程序^_^\n");
break;
default:
printf("当前输入的指令无效,请重新输入!\n");
continue;
}
}
free(pstr);
return 0;
}
============================
* 欢迎使用此程序,指令如下 *
* 1.生成一个 M*N 的矩阵 *
* 2.初始化矩阵 *
* 3.给矩阵中某个元素赋值 *
* 4.读取矩阵中的某个元素 *
* 5.打印整个矩阵 *
* 6.结束程序 *
============================
请输入指令:1
请输入新矩阵的规模(M*N)-> 3*4
3*4的新矩阵创建成功!
请输入指令:2
请输入一个数字:1
请输入指令:3
请输入要修改的位置(行/列)-> 1,2
请输入一个数字:2
请输入指令:4
请输入要读取的位置(行/列)-> 1,2
第1行,第2列的数字是:2
请输入指令:4
请输入要读取的位置(行/列)-> 2,3
Process returned -1073741819 (0xC0000005) execution time : 30.271 s
Press any key to continue.
输入指令5的时候是这样儿的
请输入指令:1
请输入新矩阵的规模(M*N)-> 3*4
3*4的新矩阵创建成功!
请输入指令:5
0 0 0 0
Process returned -1073741819 (0xC0000005) execution time : 8.240 s
Press any key to continue.
silver-crow 发表于 2022-4-11 10:38
可是输出的时候结果是内存不足
然后我把代码改成如下又变成新的问题了(没有再输出内存不足)
代码给你上了,还能找不到问题 ?
case {} 代码块。
内存怎么申请。
int (*pstr);
pstr = (int (*))calloc(M * N, sizeof(int));
怎么调用
memset(pstr, data, M * N * sizeof(int));
ba21 发表于 2022-4-11 19:53
代码给你上了,还能找不到问题 ?
case {} 代码块。
大佬,就是我copy你那个代码在codeblock上输出的结果是这样儿的
============================
* 欢迎使用此程序,指令如下 *
* 1.生成一个 M*N 的矩阵 *
* 2.初始化矩阵 *
* 3.给矩阵中某个元素赋值 *
* 4.读取矩阵中的某个元素 *
* 5.打印整个矩阵 *
* 6.结束程序 *
============================
请输入指令:1
请输入新矩阵的规模(M*N)-> 3*4
内存空间不足!
然后我改了一下之后变成了我第二次问的那个样子了{:10_262:} silver-crow 发表于 2022-4-12 16:00
大佬,就是我copy你那个代码在codeblock上输出的结果是这样儿的
============================
只提供解决问题的方案。 完整代码你自己完成。如果要看完整代码课程后面就有。
发你的代码前面就说了让你参考问题 所在。有新问题自己在看看,或重新发贴询问。
页:
[1]