繁体   English   中英

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

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

我有这个数据类型,我称之为ItemType ,它是一个字符串。

typedef <string> ItemType

我不确定在“string>”部分放什么。 我尝试了以下方法: typedef char[] ItemTypetypedef char* ItemType它们都typedef char* ItemType

Itemtype用于我在 C 中创建的数组列表,指示 Arraylist 的元素具有什么数据类型。 arraylist 本身是通用的,因为它接受任何ItemType并且在 .c 文件中实现,而Itemtype在 .h 头文件中。

编辑 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");

}

编辑 .h 头文件片段

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

这取决于您所说的“字符串”是什么意思。

如果您的意思是可以传递给strcpy()类的函数的char数组,则很容易

  typedef char ItemType[10];

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

限制是大小是固定的。 将 20 个字符写入ItemType (例如使用strcpy() )并且行为未定义。 您有责任确保不会将太多字符写入ItemType

如果您的意思是某种可以容纳任意长度字符串的类型,您的代码必须管理事物以确保数据存储足够大,您可以执行以下操作

  typedef char *ItemType;

这样做的问题是您的代码需要管理指针指向的内存。 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 */

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

typedef char* ItemType  

实际上意味着 ItemType = char*。

所以基本上你只是为 char* 创建了另一个名称。 ItemType 本身不是变量而是数据类型。 下面是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