youhebke52011
youhebuke52011
2015-09-27 04:19

c语言结构体问题 关于内存分配问题

1
  • malloc
  • c
  • 内存
  • 结构体

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


#define MAX 100
#define LEN sizeof(struct book)


typedef struct book
{
    char id[20];
    char name[30];
    float price;
}Book;

typedef struct List
{
    Book *eBook;
    int length;
}SqList;




void InitView()
{
    printf("\n\n\t%c %c %c %c %c %c %c %c %c %c %c  图 书 管 理 系 统 %c %c %c %c %c %c %c %c %c %c %c\n",
            26,26,26,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,27);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t1. 查询图书( Purchase control )\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t2. 删除图书(  price look up   )\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t3. 添加图书( Member management)\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t4. 修改图书( Financial management)\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t0. 退出系统( EXIT )\t\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c\t\t\t\t\t\t\t      %c\n",25,25);
        printf("\t%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",
            26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27);

}

void SearchBook()
{

}

void DeleteBook()
{

}

void AddBook()
{

}

void ReviseBook()
{

}

void InitList(SqList *L)
{
    L->eBook = (Book*)malloc(LEN*MAX);
    if(!L->eBook){
        printf("分配内存失败!\n");
        exit(0);
    }
}



void main()
{
    int n;
    SqList *L;
    InitList(L);

    while(1){

        InitView();
        printf("请选择你需要的功能的编号:");
        scanf("%d",&n);
        getchar();
        switch(n){
            case 1:SearchBook();break;
            case 2:DeleteBook();break;
            case 3:AddBook();break;
            case 4:ReviseBook();break;
            case 0:exit(0);break;
            default :printf("输入有误,请重新输入!\n");system("pause");system("cls");
        }
    }

}

为什么这样分配内存不行呢? 黑框弹出一回儿就说这程序已经停止工作了
其中LEN是sizeof(Book) ,MAX 为100

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答