Here is my code in C, I was declaring L as a non-pointer variable but then after running the program I realized that it didn't really change the values in the array after calling the Insert function. So I changed the declaration of L as
SeqList* L
where I put an extra * sign and correspondingly changed those . to -> , but now I keep getting the
Segmentation fault (core dumped)
message? Where did I miss something? Thanks!
#include "stdio.h"
#define MAXSIZE 100
typedef struct SeqList{
int elem[MAXSIZE];
int last;
} SeqList;
int GetData(SeqList* L,int i){
return L->elem[i];
}
void Insert(SeqList* L, int i, int e){
int temp;
if (i < 1 || i > L->last + 2){
printf("Invalid Inserting point.\n");
}
if (L->last > MAXSIZE){
printf("List already full.\n");
}
for(temp = L->last; temp != i; temp--){
L->elem[temp+1] = L->elem[temp];
}
L->elem[temp] = e;
L->last++;
}
int main(){
SeqList* L;
int i;
for(i=0;i<10;i++){
L->elem[i] = i*i;
}
Insert(L,5,10);
for(i=0;i<10;i++){
printf("%d\n", L->elem[i]);
}
printf("%d\n", GetData(L,5));
}
-gif you’re using GCC or Clang) and running it under a debugger (probably GDB). It should tell you where it crashes. Add that to your question.SeqList* L, but not allocate space for it