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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3