warning C4700:local variable 'l' used without having been in

1个回答

  • 你的main函数中的l变量应作为全局变量,放在最最开始的那几行,在执行完函数时的过程中,l中三元素的值当然会变,但执行完,l就有返回执行前的状态了,函数在执行时会为括号中的各个参数创建一块空间,但执行完毕之后就会自动释放.例如你执行status status Initlist(sqlist l,int list_number)时,编译器先为l建一个复制品,名字也叫l,在执行完这个函数后,那个复制的l就没了,只剩下原来的l,你的status listinsert_sq(sqlist l,int i,elemtype e )也是这个毛病,++l.length只在函数中改变了那个复制品的length值,执行完这个函数后l.length还是原来那个l.length(比预期的总少一),导致在执行插入操作后,程序输出总是少了最后一个数.

    warning C4700:local variable 'l' used without having been initialized的意思是没对l初始化,initialized不是init的形容词形式吗?(不严格的说)你想想,是不是你的status Initlist(sqlist l,int list_number)这个函数只对你那个复制品l初始化了,其实l并没被初始化.

    我写了一个没把l放在main()外面做全局变量的程序,你看看是不是你想要的:

    #include "stdio.h"

    #include "stdlib.h"

    #define ERROR 0

    #define ok 1

    #define OVERFLOW -2

    #define list_init_size 100

    #define listincrement 10

    typedef int status;

    typedef int elemtype;

    typedef struct{

    elemtype *elem;

    int length;

    int listsize;

    }sqlist;

    status Initlist(sqlist l,int list_number)

    {if(list_number>list_init_size) return ERROR;

    l.elem=(elemtype*)malloc(list_number*sizeof(elemtype));

    if(!l.elem) exit(OVERFLOW);

    l.length=0;

    l.listsize=list_number;

    return ok;

    }

    status listinsert_sq(sqlist l,int i,elemtype e )

    {

    elemtype *p,*q,*newbase;

    if(il.length+1) return ERROR;

    if(l.length>=l.listsize)

    {

    newbase=(elemtype*)realloc(l.elem,(l.listsize+listincrement)*sizeof(elemtype));

    if(!newbase) return(OVERFLOW);

    l.elem=newbase;

    l.listsize+=listincrement;

    }

    q=&(l.elem[i-1]);

    for(p=&(l.elem[l.length-1]);p>=q;--p) {*(p+1)=*p;}

    *q=e;

    //改动处

    //++l.length;(这句话就没用了,写不写都成)

    //改动处

    return ok;

    }

    void outputlist(sqlist l)

    {int j;

    for(j=0;jlist_init_size) return ERROR;

    l.elem=(elemtype*)malloc(n*sizeof(elemtype));

    if(!l.elem) exit(OVERFLOW);

    l.length=0;

    l.listsize=n;

    //改动处

    printf("输入线性表元素:");

    for(i=0;ilist_init_size) exit(0);//return ERROR;

    l.elem=(elemtype*)malloc(list_number*sizeof(elemtype));

    if(!l.elem) exit(OVERFLOW);

    l.length=0;

    l.listsize=list_number;

    //return ok;

    }

    //改动处

    /*status*/void listinsert_sq(int i,elemtype e )

    //改一下返回类型

    //改动处

    {

    elemtype *p,*q,*newbase;

    if(il.length+1) exit(0);//return ERROR;

    if(l.length>=l.listsize)

    {

    newbase=(elemtype*)realloc(l.elem,(l.listsize+listincrement)*sizeof(elemtype));

    if(!newbase) exit(OVERFLOW);

    l.elem=newbase;

    l.listsize+=listincrement;

    }

    q=&(l.elem[i-1]);

    for(p=&(l.elem[l.length-1]);p>=q;--p) *(p+1)=*p;

    *q=e;

    ++l.length;

    //return ok;

    }

    void outputlist()

    {int j;

    for(j=0;j