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日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度