简体   繁体   中英

How to create a type definition for a string in c

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);

It depends on what you mean by "string".

If you mean an array of char that can be passed to functions like strcpy() , it is easy

  typedef char ItemType[10];

declares ItemType to be an array of char , which can hold any C-style string for which strlen() returns 9 or less (the difference of 1 is due to the terminating '\\0' terminator that string related functions ( strcmp() , strcat() , strlen() ) look for to mark the end of the string).

The limitation is that the size is fixed. Write 20 characters to an ItemType (eg using strcpy() ) and behaviour is undefined. It is your responsibility to ensure too many characters are not written to an ItemType .

If you mean some type that can hold an arbitrary length string, where YOUR code has to manage things to ensure the data storage is large enough, you can do things like

  typedef char *ItemType;

The problem with this is that your code needs to manage the memory that pointer points at. ItemType is a pointer, not an array which can hold data.

 #include <stdlib.h>    /*  declares malloc(), realloc(), free(), etc */

 /*   and in a function somewhere */
 
 ItemType x = malloc(25);

  /*   can use any operations that do not copy more than 24 characters to x  */
  /*  but if we want a larger string, we have to manage it   */

  ItemType temp = realloc(x, 50);

  if (temp == NULL)   /*  reallocation failed */
  {
       /* work out how to recover or terminate */
  }
  else
  {
       x = temp;
  } 

   /*  If reallocation failed and no recovery is done, do not execute the following code */

  /* can now treat x as an array of 50 characters (i.e. if we ensure strlen(x) never exceeds 49 */


  free(x);    /*  when we are done with x */

If you want a string type that will resize itself as needed (eg as provided by some other languages) then there is no way in C.

typedef char* ItemType  

actually means ItemType = char*.

So basically you have created just another name for char*. ItemType itself is not a variable but a data type. Following is the usage of typedef

int main()
{
    typedef char* itemType;

    itemType str = (char*) malloc(50);
    strcpy(str, "Hello World");

    printf("%s\n", str);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM