简体   繁体   English

如何在c中为字符串创建类型定义

[英]How to create a type definition for a string in c

I have this datatype I call ItemType which is a string.我有这个数据类型,我称之为ItemType ,它是一个字符串。

typedef <string> ItemType

I am not sure what to put into the "string>" part.我不确定在“string>”部分放什么。 I've tried the following: typedef char[] ItemType and typedef char* ItemType neither of them works.我尝试了以下方法: typedef char[] ItemTypetypedef char* ItemType它们都typedef char* ItemType

Itemtype is used for an arraylist I am creating in C, indicating what datatype the elements of the Arraylist has. Itemtype用于我在 C 中创建的数组列表,指示 Arraylist 的元素具有什么数据类型。 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. arraylist 本身是通用的,因为它接受任何ItemType并且在 .c 文件中实现,而Itemtype在 .h 头文件中。

EDIT 1 .c Implementation code snippets:编辑 1 .c 实现代码片段:

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编辑 .h 头文件片段

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如果您的意思是可以传递给strcpy()类的函数的char数组,则很容易

  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).声明了ItemType是阵列char ,其可以保持其中任何C风格串strlen()返回9或更小(的差1是由于终止'\\0'终止子串相关的功能( strcmp()strcat() , strlen() ) 寻找标记字符串的结尾)。

The limitation is that the size is fixed.限制是大小是固定的。 Write 20 characters to an ItemType (eg using strcpy() ) and behaviour is undefined.将 20 个字符写入ItemType (例如使用strcpy() )并且行为未定义。 It is your responsibility to ensure too many characters are not written to an ItemType .您有责任确保不会将太多字符写入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. ItemType是一个指针,而不是一个可以保存数据的数组。

 #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.如果您想要一个可以根据需要调整自身大小的字符串类型(例如,由其他一些语言提供),那么在 C 中是没有办法的。

typedef char* ItemType  

actually means ItemType = char*.实际上意味着 ItemType = char*。

So basically you have created just another name for char*.所以基本上你只是为 char* 创建了另一个名称。 ItemType itself is not a variable but a data type. ItemType 本身不是变量而是数据类型。 Following is the usage of typedef下面是typedef的用法

int main()
{
    typedef char* itemType;

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

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

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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