dingbuzhubambo 2020-07-01 19:53 采纳率: 20%
浏览 224
已采纳

c 线性表初始化 代码错误 异常 看不懂原因了。。。。。。

1.线性表初始化 代码存在问题 vc++6.0

2.代码

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

#define  Listsize 20
typedef    int  ElemType;


typedef struct {
ElemType  * elem;
int length;
int listsize;
}Sqlist,*Sq;

Sq InitSq(){
    Sq L;
    L->elem=(ElemType *) malloc(sizeof(ElemType )*Listsize);
    L->length=0;
    L->listsize=Listsize;

    int  i=0;
    for(i =0; i<12 ;++i)
    {
    L->elem[i]=5*i;
    L->length++;
    }

    return L;
}


int main(){

    Sq l=InitSq();
    for(j=0;j<Listsize;j++) 
        printf(l->elem[j]);

    return 0;

}

3.错误:
error C2143: syntax error : missing ';' before 'type'
D:\demo\cdemo\1.c(21) : error C2065: 'i' : undeclared identifier
D:\demo\cdemo\1.c(38) : error C2143: syntax error : missing ';' before 'type'
D:\demo\cdemo\1.c(39) : error C2065: 'j' : undeclared identifier
D:\demo\cdemo\1.c(40) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
D:\demo\cdemo\1.c(40) : warning C4024: 'printf' : different types for formal and actual parameter 1

实在不知道是什么原因了 麻烦方便的话 能够解答一下 谢谢

  • 写回答

3条回答 默认 最新

  • 九头蛇daze 2020-07-02 10:08
    关注

    指针类型要做动态分配,修改了代码,你试试

    #include <stdio.h>
    #include <stdlib.h>
    
    #define  Listsize 20
    typedef    int  ElemType;
    
    
    typedef struct {
        ElemType  * elem;
        int length;
        int listsize;
    }Sqlist, *Sq;
    
    Sq InitSq(){
        int  i = 0;
        Sq L = (Sq)malloc(sizeof(Sqlist));
        L->elem = (ElemType *)malloc(sizeof(ElemType)*Listsize);
        L->length = 0;
        L->listsize = Listsize;
    
        for (i = 0; i < 12; ++i)
        {
            L->elem[i] = 5 * i;
            L->length++;
        }
    
        return L;
    }
    
    
    int main(){
    
        int j;
        Sq l = InitSq();
        for (j = 0; j < l->length; j++)
            printf("%d ",l->elem[j]);
    
        free(l);
        return 0;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器