zoukankan      html  css  js  c++  java
  • C 语言的一个错误,没找出原因

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int
    main(void)
    {
    char str[512];
    char *endpstr;
    int N;
    struct struct_num {
    int highs[N];
    int violate[N];
    };

    struct struct_num num;
    /* 获取N */
    scanf("%d", &N);
    getchar();

    /* 获取身高数据 */
    fgets(str, 512, stdin);

    endpstr = str;
    int cur = 0
    /* while (endpstr) { */
    /* num.highs[i++] = strtol(endpstr, &endpstr, 10); */
    /* printf("%d ", strtol(endpstr, &endpstr, 10)); */
    ;
    /* } */

    return 0;
    }

    这段代码是可以编译通过的,但是,只要我把512, 换成1024,1022,都会得到

    Segmentation fault (core dumped)

    没有任何要求我输入,直接显示错误哦


    如果换乘

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int
    main(void)
    {
    char str[1024];
    char *endpstr;
    int N;
    struct struct_num {
    int highs[N];
    int violate[N];
    };
    int cur;
    struct struct_num num;
    /* 获取N */
    scanf("%d", &N);
    getchar();

    /* 获取身高数据 */
    fgets(str, 1024, stdin);

    endpstr = str;
    cur = 0;
    while (endpstr) {
    /* num.highs[i++] = strtol(endpstr, &endpstr, 10); */
    printf("%d ", strtol(endpstr, &endpstr, 10));
    /* ; */
    }

    return 0;
    }

    会显示的错误是:

    Bus error (core dumped)

    如果换成512,也会能够编译通过。这是为什么?

  • 相关阅读:
    IOC+AOP
    基础知识
    断点续传
    监听程序
    Action、View、ActionResult、ViewResult、ContentResult
    json的使用(JObect,JsonData,JArray)
    get/post 接口调用
    常见的加密和解密
    WebUtility(提供在处理 Web 请求时用于编码和解码 URL 的方法。)
    MyBatis动态SQL和缓存
  • 原文地址:https://www.cnblogs.com/hwy89289709/p/7229049.html
Copyright © 2011-2022 走看看