鱼C论坛

 找回密码
 立即注册
查看: 4004|回复: 6

[已解决]新手求助,c语言学习s1e38动动手的最后一题

[复制链接]
发表于 2022-4-9 23:22:09 | 显示全部楼层 |阅读模式
30鱼币
#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)[N];
                pstr = (int (*)[N])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[m - 1][n - 1] = 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[m - 1][n - 1]);
                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[i][j]);
                    }
                    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 秒) ===|
最佳答案
2022-4-9 23:22:10
自己看吧。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     printf("============================\n");
  8.     printf("* 欢迎使用此程序,指令如下 *\n");
  9.     printf("* 1.生成一个 M*N 的矩阵    *\n");
  10.     printf("* 2.初始化矩阵             *\n");
  11.     printf("* 3.给矩阵中某个元素赋值   *\n");
  12.     printf("* 4.读取矩阵中的某个元素   *\n");
  13.     printf("* 5.打印整个矩阵           *\n");
  14.     printf("* 6.结束程序               *\n");
  15.     printf("============================\n");

  16.     int order = 0, indicator = 0, data;
  17.     int M, N, m, n, i, j;
  18.     //M,N用来表示矩阵规模
  19.     //m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
  20.     //data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
  21.     //i,j用来作为输出二维数组的标志
  22.     char judge;

  23.                 int (*pstr)[N];
  24.             pstr = (int (*)[N])calloc(M * N, sizeof(int));

  25.     while(order != 6)
  26.     {
  27.         printf("请输入指令:");
  28.         scanf("%d", &order);

  29.         switch(order)
  30.         {
  31.         case 1:
  32.         {
  33.             if(indicator)
  34.             {
  35.                 printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
  36.                 scanf("%c", &judge);
  37.                 if(judge == 'N')
  38.                 {
  39.                     continue;
  40.                 }
  41.             }
  42.             printf("请输入新矩阵的规模(M*N)-> ");
  43.             scanf("%d*%d", &M, &N);


  44.             if(pstr == NULL)
  45.             {
  46.                 printf("内存空间不足!\n");
  47.                 exit(1);
  48.             }
  49.             printf("%d*%d的新矩阵创建成功!\n", M, N);
  50.             indicator = 1;
  51.             break;
  52.         }
  53.         case 2:
  54.         {
  55.             if(!indicator)
  56.             {
  57.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  58.                 continue;
  59.             }

  60.             printf("请输入一个数字:");
  61.             scanf("%d", &data);

  62.             memset(pstr, data, M * N * sizeof(int));
  63.             break;
  64.         }
  65.         case 3:
  66.         {
  67.             if(!indicator)
  68.             {
  69.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  70.                 continue;
  71.             }

  72.             printf("请输入要修改的位置(行/列)-> ");
  73.             scanf("%d,%d", &m, &n);

  74.             printf("请输入一个数字:");
  75.             scanf("%d", &data);

  76.             pstr[m - 1][n - 1] = data;
  77.             break;
  78.         }
  79.         case 4:
  80.         {
  81.             if(!indicator)
  82.             {
  83.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  84.                 continue;
  85.             }

  86.             printf("请输入要读取的位置(行/列)-> ");
  87.             scanf("%d,%d", &m, &n);
  88.             printf("第%d行,第%d列的数字是:%d\n", m, n, pstr[m - 1][n - 1]);
  89.             break;
  90.         }
  91.         case 5:
  92.         {
  93.             if(!indicator)
  94.             {
  95.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  96.                 continue;
  97.             }

  98.             for(i = 0; i < M; i++)
  99.             {
  100.                 for(j = 0; j < N; j++)
  101.                 {
  102.                     printf("%4d", pstr[i][j]);
  103.                 }
  104.                 printf("\n");
  105.             }
  106.             break;
  107.         }
  108.         case 6:
  109.         {

  110.             printf("感谢使用本程序^_^\n");
  111.             break;
  112.         }
  113.         default:
  114.         {

  115.             printf("当前输入的指令无效,请重新输入!\n");
  116.             continue;
  117.         }
  118.         }
  119.     }

  120.     free(pstr);

  121.     return 0;
  122. }
复制代码

最佳答案

查看完整内容

自己看吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-9 23:22:10 | 显示全部楼层    本楼为最佳答案   
自己看吧。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     printf("============================\n");
  8.     printf("* 欢迎使用此程序,指令如下 *\n");
  9.     printf("* 1.生成一个 M*N 的矩阵    *\n");
  10.     printf("* 2.初始化矩阵             *\n");
  11.     printf("* 3.给矩阵中某个元素赋值   *\n");
  12.     printf("* 4.读取矩阵中的某个元素   *\n");
  13.     printf("* 5.打印整个矩阵           *\n");
  14.     printf("* 6.结束程序               *\n");
  15.     printf("============================\n");

  16.     int order = 0, indicator = 0, data;
  17.     int M, N, m, n, i, j;
  18.     //M,N用来表示矩阵规模
  19.     //m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
  20.     //data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
  21.     //i,j用来作为输出二维数组的标志
  22.     char judge;

  23.                 int (*pstr)[N];
  24.             pstr = (int (*)[N])calloc(M * N, sizeof(int));

  25.     while(order != 6)
  26.     {
  27.         printf("请输入指令:");
  28.         scanf("%d", &order);

  29.         switch(order)
  30.         {
  31.         case 1:
  32.         {
  33.             if(indicator)
  34.             {
  35.                 printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
  36.                 scanf("%c", &judge);
  37.                 if(judge == 'N')
  38.                 {
  39.                     continue;
  40.                 }
  41.             }
  42.             printf("请输入新矩阵的规模(M*N)-> ");
  43.             scanf("%d*%d", &M, &N);


  44.             if(pstr == NULL)
  45.             {
  46.                 printf("内存空间不足!\n");
  47.                 exit(1);
  48.             }
  49.             printf("%d*%d的新矩阵创建成功!\n", M, N);
  50.             indicator = 1;
  51.             break;
  52.         }
  53.         case 2:
  54.         {
  55.             if(!indicator)
  56.             {
  57.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  58.                 continue;
  59.             }

  60.             printf("请输入一个数字:");
  61.             scanf("%d", &data);

  62.             memset(pstr, data, M * N * sizeof(int));
  63.             break;
  64.         }
  65.         case 3:
  66.         {
  67.             if(!indicator)
  68.             {
  69.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  70.                 continue;
  71.             }

  72.             printf("请输入要修改的位置(行/列)-> ");
  73.             scanf("%d,%d", &m, &n);

  74.             printf("请输入一个数字:");
  75.             scanf("%d", &data);

  76.             pstr[m - 1][n - 1] = data;
  77.             break;
  78.         }
  79.         case 4:
  80.         {
  81.             if(!indicator)
  82.             {
  83.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  84.                 continue;
  85.             }

  86.             printf("请输入要读取的位置(行/列)-> ");
  87.             scanf("%d,%d", &m, &n);
  88.             printf("第%d行,第%d列的数字是:%d\n", m, n, pstr[m - 1][n - 1]);
  89.             break;
  90.         }
  91.         case 5:
  92.         {
  93.             if(!indicator)
  94.             {
  95.                 printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
  96.                 continue;
  97.             }

  98.             for(i = 0; i < M; i++)
  99.             {
  100.                 for(j = 0; j < N; j++)
  101.                 {
  102.                     printf("%4d", pstr[i][j]);
  103.                 }
  104.                 printf("\n");
  105.             }
  106.             break;
  107.         }
  108.         case 6:
  109.         {

  110.             printf("感谢使用本程序^_^\n");
  111.             break;
  112.         }
  113.         default:
  114.         {

  115.             printf("当前输入的指令无效,请重新输入!\n");
  116.             continue;
  117.         }
  118.         }
  119.     }

  120.     free(pstr);

  121.     return 0;
  122. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-10 10:42:08 | 显示全部楼层
为啥你是新手
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-11 10:38:20 | 显示全部楼层

可是输出的时候结果是  内存不足

然后我把代码改成如下又变成新的问题了(没有再输出内存不足)

#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)[N];
    //pstr = (int (*)[N])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 (*)[N])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[m - 1][n - 1] = 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[m - 1][n - 1]);
                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[i][j]);
                    }
                    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.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-11 19:53:39 | 显示全部楼层
silver-crow 发表于 2022-4-11 10:38
可是输出的时候结果是  内存不足

然后我把代码改成如下又变成新的问题了(没有再输出内存不足)

代码给你上了,还能找不到问题 ?
case {} 代码块。

内存怎么申请。
      int (*pstr)[N];
            pstr = (int (*)[N])calloc(M * N, sizeof(int));

怎么调用
memset(pstr, data, M * N * sizeof(int));
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-12 16:00:07 | 显示全部楼层
ba21 发表于 2022-4-11 19:53
代码给你上了,还能找不到问题 ?
case {} 代码块。

大佬,就是我copy你那个代码在codeblock上输出的结果是这样儿的

============================
* 欢迎使用此程序,指令如下 *
* 1.生成一个 M*N 的矩阵    *
* 2.初始化矩阵             *
* 3.给矩阵中某个元素赋值   *
* 4.读取矩阵中的某个元素   *
* 5.打印整个矩阵           *
* 6.结束程序               *
============================
请输入指令:1
请输入新矩阵的规模(M*N)-> 3*4
内存空间不足!

然后我改了一下之后变成了我第二次问的那个样子了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-12 16:21:26 | 显示全部楼层
silver-crow 发表于 2022-4-12 16:00
大佬,就是我copy你那个代码在codeblock上输出的结果是这样儿的

============================

只提供解决问题的方案。 完整代码你自己完成。如果要看完整代码课程后面就有。
发你的代码前面就说了让你参考问题 所在。有新问题自己在看看,或重新发贴询问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 05:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表