I am trying to implement a java like arraylist in C and now I am trying to make it generic. I am new to C and pointer arithmetic but I triedusing void* pointer for container, then element size(for size of data type), capacity and size. Currently in my add method I am getting an error saying (expression must have struct or union type in the add method inside of memcpy). As I understand memcpy, the first parameter is the destination where i want to put my input so i iterate from start of my container to elementSize*numElements ahead to reach the last index to append my input. Then the third parameter is the size of input. And is the resize function alright? I am new to C and playing around for educational purposes and any help is appreciated. Thank you
_____________________________________________________________________________________________
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
typedef struct ArrayList ArrayList;
typedef int bool;
#define false 0
#define true 1
struct ArrayList {
void *con;
int elementSize;
int numElements;
int conSize;
};
ArrayList *createArrayList();
void freeArrayList(ArrayList *);
void add(ArrayList *, void*);
#endif
___________________________________________________________________________________________________
#include<stdio.h>
#include<stdlib.h>
#include"Consts.h"
#include "ArrayList.h"
#define CAPACITY 5
ArrayList *createArrayList(int elementSize) {
ArrayList *arrayList = malloc(sizeof(ArrayList));
arrayList->elementSize = elementSize;
arrayList->conSize = CAPACITY;
arrayList->numElements = 0;
arrayList->con = malloc(sizeof(elementSize) * CAPACITY);
return arrayList;
}
void freeArrayList(ArrayList * arrayList) {
if (arrayList == NULL) {
return;
}else {
free(arrayList->con);
free(arrayList);
}
}
void add(ArrayList *list, void *input) {
if (list->numElements >= list->conSize) {
resize(list);
printf("resized\n");
}
memcpy(list->con + (list.numElements) * (list.elementSize), input, list->elementSize * list->numElements);
list->numElements++;
}
void resize(ArrayList *list) {
void *temp = realloc(list->con, (list->conSize + 100) * list->elementSize);
if (temp != NULL) {
list->conSize += 100;
list->con = temp;
}
}