开发者社区 问答 正文

用*&当参数时报错,去掉&就可以

void InitList(SqList &L)//初始化列表其中&L为引用型指针
{
L = (SqList*)malloc(sizeof(SqList));
L->length = 0;
}

image.png 这里加&报错

展开
收起
海边一只船 2020-05-27 15:57:24 736 分享 版权
1 条回答
写回答
取消 提交回答
  • 怎么报的错,本地试了一下,无问题。

    #include <iostream>
    #include <stdlib.h> 
    
    using namespace std;
    
    struct SqList
    {
        int length;
    };
    
    void InitList(SqList *&L)//初始化列表其中&L为引用型指针
    {
        L = (SqList*)malloc(sizeof(SqList));
        L->length = 12;
    }
    
    int main() {
        SqList *L;
        InitList(L);
        cout << L->length << endl;
        return 0;
    }
    
    

    g++ hello.cpp -o hello 编译

    2020-05-27 17:52:14
    赞同 展开评论
问答地址: