Why `&` is not allowed in C, but in C++ in this case? Is there any defference? [duplicate]

  c++

Here is my C code, I use void InitList(List &L); However Code::Blocks`s buildlog has an error:

expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef int ElementType;

struct SeqList;
typedef struct SeqList List;
void InitList(List &L);

struct SeqList
{
    ElementType *data;
    int CurLength;
};

/*----------------------------------*/

void InitList(List &L)
{
    (&L)->data = malloc(sizeof(ElementType)*MaxSize); 
    (&L)->CurLength = 0;
}


int main()
{
    List L;
    InitList(&L);
    return 0;
}

But I tried in C++, there is no error:

#include <iostream>
using namespace std;
#define InitSize 100
typedef int ElementType;

struct SeqList;
typedef struct SeqList List;
void InitList(List &L);

struct SeqList
{
    ElementType *data;
    int CurLength;
};


/*----------------------------------*/

void InitList(List &L)
{
    L.data = new ElementType[InitSize]; //L.data = malloc(...)
    L.CurLength = 0;
}

int main()
{
    List L;
    InitList(L);
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT