Sasakisouji 2022-06-05 10:37 采纳率: 71.4%
浏览 17
已结题

分函数访问全局指针出现问题

创建的全局变量指针,用分函数访问时发生位置冲突,请问这是为什么

#define _CRT_SECURE_NO_WARNINGS

#include "stdio.h"
#include "stdlib.h"

struct bustype
{
    int ntype;
    int   bus_no;
    int   occurtime;
    struct bustype *next;
    struct bustype *prior;
}*s, *top, *base, *l, *head, *tail;

typedef struct _excel
{
    int bus_no;
    int arrivetime;
    int leavetime;
    char placetype;
    _excel *next;
    //_excel *prior;
}_excel;

struct _flag
{
    int num_s;
    int num_l;
    int num_excel;

}flag;

_excel *excel, *event;

int createstack()
{
    
    s = (struct bustype *)malloc(sizeof(bustype *));
    l = (struct bustype *)malloc(sizeof(bustype *));
    event = (_excel *)malloc(sizeof(_excel *));
    s = top = base;
    l = head = tail;
    struct bustype s_tem[7];
    flag.num_s = 0;
    flag.num_l = 0;
    flag.num_excel = 0;
    s->prior = NULL;
    l->prior = NULL;
    excel->next = event;
    return 0;
}

int check(int bus__no,char placetype,struct bustype *pre)
{
    struct bustype *p;//临时节点
    int i;
    p = base;
    switch (placetype)
    {
    case 's':
    {
        if (s->ntype == 1)
        {
            s->next = (struct bustype *)malloc(sizeof(bustype *));
            s->next->prior = s;
            s = s->next;
            top = s;
            flag.num_s++;
        }
        else
        {
            for (i = 1; i < flag.num_s;i++)
            {
                if (p->bus_no == bus__no)
                {
                    if (p->prior == NULL)
                    {
                        p = p->next;
                        free(p->prior);
                    }
                    else
                    {
                        p->prior->next = p->next;
                        p->next->prior = p->prior;
                        p = p->next;
                        free(p->prior->next);
                        p->prior->next = p;
                    }
                    flag.num_excel++;
                    flag.num_s--;
                    event->bus_no = bus__no;
                    event->arrivetime = s->occurtime;
                    event->leavetime = pre->occurtime;
                    event->placetype = '普';
                    /*excel->arrivetime=event->arrivetime;
                    excel->bus_no = event->bus_no;
                    excel->leavetime = event->leavetime;
                    excel->placetype = event->placetype;*/
                    event = event->next;
                    event = (_excel *)malloc(sizeof(_excel *));

                    
                }
            }
        }
        break;
    }
    case 'l':
    {
        if (l->ntype == 1)
        {
            p = tail;
            l = l->next;
            l = (struct bustype *)malloc(sizeof(bustype *));
            tail = l;
            flag.num_l++;
        }
        else
        {
            for (i = 1; i < flag.num_l; i++)
            {
                if (p->bus_no == bus__no)
                {
                    if (p->prior == NULL)
                    {
                        p = p->next;
                        free(p->prior);
                    }
                    else
                    {
                        p->prior->next = p->next;
                        p->next->prior = p->prior;
                        p = p->next;
                        free(p->prior->next);
                        p->prior->next = p;
                    }
                    flag.num_excel++;
                    flag.num_l--;
                    event->bus_no = bus__no;
                    event->arrivetime = l->occurtime;
                    event->leavetime = pre->occurtime;
                    event->placetype = '便道';
                    event = event->next;
                    event = (_excel *)malloc(sizeof(_excel *));


                }
            }
        }
        break;
    }
    }
    return 0;
}

int input()
{
    do{
        if (flag.num_s < 7)
        {
            scanf("{%d,%d,%d}", s->ntype, s->bus_no, s->occurtime);
            check(s->bus_no, 's', s);//判断
        }
        else
        {
            scanf("{%d,%d,%d}", l->ntype, l->bus_no, l->occurtime);
            check(l->bus_no, 'l', l);
        }
    } while (s->ntype == 6 || l->ntype == 6);
    return 0;
}

int main()
{
    createstack();
    input();
}

img

  • 写回答

1条回答 默认 最新

  • Jacob*y 2022-06-05 13:46
    关注

    分函数是什么意思?这错误应该是访问了不该访问的内存导致

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月13日
  • 已采纳回答 6月5日
  • 创建了问题 6月5日

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系