How to create a type definition for a string in c

Viewed 18013

I have this datatype I call ItemType which is a string.

typedef <string> ItemType

I am not sure what to put into the "string>" part. I've tried the following: typedef char[] ItemType and typedef char* ItemType neither of them works.

Itemtype is used for an arraylist I am creating in C, indicating what datatype the elements of the Arraylist has. The arraylist itself is generic as it accepts whatever ItemType is and is implemented in a .c file while Itemtype is in the .h header file.

EDIT 1 .c Implementation code snippets:

struct list_type {
    ItemType* data;
    int size;  
    int capacity;
};


// adds a component to the array, if enough memory available

void push(ListType listptr, ItemType item) {  
    if (listptr->size >= listptr->capacity) {
        ItemType * temp = malloc(sizeof(ItemType) * (listptr->capacity + 200));
        if (temp != NULL) {
              listptr->capacity += 200;
              memcpy(temp, listptr->data,sizeof(ItemType) * (listptr->size));
              free(listptr->data);
              listptr->data = temp;
              listptr->data[listptr->size] = item;
              listptr->size++;
              printf("%s inserted:%s ", item, listptr->data[listptr->size]);
        }
    }

    else {
         listptr->data[listptr->size] = item;
         listptr->size++;
         printf("%s inserted:%s ", item, listptr->data[listptr->size]);
    }

}

void printl(ListType listptr) {
  int i;
  for(i = 0; i < listptr->size; i++) printf("%s ", listptr->data[i]);
  printf("\n");

}

EDIT .h header file snippets

typedef struct list_type *ListType;
typedef char* ItemType;
//other code
void push(ListType l, ItemType item);
2 Answers
Related