sean sean - 1 year ago 280
C Question

C linked list Process returned 255 (0xFF) execution time 2.144s

I am writing a sequence linked list in C language.

Execution result´╝Ü Process returned 255 (0xFF) execution time 2.144s

The struct contains array component and use 'typedef' struct as pointer type, what's wrong with it, can anyone help me?

#include <stdio.h>
#define OK 1
#define ERROR 0
#define ERROR_EXIT -1
#define MAXSIZE 30

typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int len;
}*SQLIST;

Status listInit(SQLIST L)
{

int len = 10;
int i = 0;
for(;i<len;i++)
{
L->data[i] = i; // There is problem here
}

L->len = 10;
return OK;
}

Status listShow(SQLIST L)
{

return OK;
}

int main()
{
SQLIST L;
listInit(L);
printf("%d\n",L->len);
return OK;
}

Answer Source

You should define the struct as:

typedef struct
{
    ElemType data[MAXSIZE];
    int len;
} SQLIST, *PSQLIST;

Now, you can allocate L in main() like so:

PSQLIST L = malloc(sizeof(SQLIST));

Don't forget to free(L) when you're done with it, and rename all your current instances of SQLIST to PSQLIST.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download