Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用Typedef结构创建函数以返回拆分字符串数组_C_Pointers_Dynamic Memory Allocation_Strtok - Fatal编程技术网

C 如何使用Typedef结构创建函数以返回拆分字符串数组

C 如何使用Typedef结构创建函数以返回拆分字符串数组,c,pointers,dynamic-memory-allocation,strtok,C,Pointers,Dynamic Memory Allocation,Strtok,在解决练习时,我被内存分配所困扰。本练习的目标是创建一个函数,通过使用分隔符(在本例中是第二个参数)拆分字符串,然后返回包含新拆分字符串的typedef结构中的数组,该数组包含两个参数(例如“abc def gh-!”&&&&&-”) 这是我到目前为止的代码 #ifndef STRUCT_STRING_ARRAY #define STRUCT_STRING_ARRAY typedef struct s_string_array { int size; char** arr

在解决练习时,我被内存分配所困扰。本练习的目标是创建一个函数,通过使用分隔符(在本例中是第二个参数)拆分字符串,然后返回包含新拆分字符串的typedef结构中的数组,该数组包含两个参数(例如“abc def gh-!”&&&&&-”)

这是我到目前为止的代码

    #ifndef STRUCT_STRING_ARRAY
#define STRUCT_STRING_ARRAY
typedef struct s_string_array
{
    int size;
    char** array;
} string_array;
#endif

string_array* my_split(char* str, char* sep) {

  string_array*ptr=(string_array*)malloc(sizeof(string_array)); //Memory allocation Struct

  int i;
  int j;
  int words;
  int in_word;

  i = 0;
  words = 1;

  while (str[i]) {
      if (str[i] != *sep) {
          if (!in_word) {
              words++; // Count number of words inside the string
          }
          in_word = 1;
      } else {
          in_word = 0;
      }
      i++;
  }
  ptr->size = words;
  ptr->array=malloc(sizeof(char*)*ptr->size); // Allocate the array of pointer inside struct

  int size = 0;
  i = 0;
  j = 0;
  while (i < ptr->size)  {
      while (str[j] != *sep) {
          size++;
          j++;
      }
      ptr->array[i]=malloc(sizeof(char) * (size + 1));
      ptr->array[i][size+1] = '\0';
    i++;
  }

  int c = 0;
  int r = 0;
  i = 1;
  j = 0;

  while (i < ptr->size) {
      if (str[j] != *sep) {
          while (str[j] != *sep) {
              ptr->array[c][r++] = str[j++];
          }
      }
      i++;
      c++;
  }
  printf("%s\n", ptr->array[0]);
  printf("Words in new Array is: %d\n", ptr->size);
  printf("J is at index: %d\n", j);
  printf("The character at index J is: %c\n", str[j]);
  printf("The first index of the array is now at: %d\n", c);
}

int main() {
    my_split("abc def gh-!", "-");
    return 0;
}
\ifndef结构字符串数组
#定义结构字符串数组
typedef结构s_字符串_数组
{
整数大小;
字符**数组;
}字符串数组;
#恩迪夫
字符串数组*我的拆分(char*str,char*sep){
string_array*ptr=(string_array*)malloc(sizeof(string_array));//内存分配结构
int i;
int j;
智力词;
int-in_-word;
i=0;
单词=1;
while(str[i]){
如果(str[i]!=*sep){
如果(!in_word){
words++;//计算字符串中的字数
}
in_word=1;
}否则{
in_word=0;
}
i++;
}
ptr->size=字;
ptr->array=malloc(sizeof(char*)*ptr->size);//在结构内部分配指针数组
int size=0;
i=0;
j=0;
而(isize){
while(str[j]!=*sep){
大小++;
j++;
}
ptr->array[i]=malloc(sizeof(char)*(size+1));
ptr->数组[i][size+1]='\0';
i++;
}
int c=0;
int r=0;
i=1;
j=0;
而(isize){
如果(str[j]!=*sep){
while(str[j]!=*sep){
ptr->array[c][r++]=str[j++];
}
}
i++;
C++;
}
printf(“%s\n”,ptr->array[0]);
printf(“新数组中的单词是:%d\n”,ptr->size);
printf(“J位于索引:%d\n”,J);
printf(“索引J处的字符为:%c\n”,str[J]);
printf(“数组的第一个索引现在位于:%d\n”,c);
}
int main(){
我的分裂(“abc定义gh-!”,“-”);
返回0;
}
返回值必须为:[“abc def gh”,“!”]


请帮助。

ptr->array[i]=malloc(sizeof(char)*(size+1));ptr->数组[i][size+1]='\0'Oops,超出写入范围!分隔符是字符指针吗,因为它可能包含多个分隔符?分隔符是字符串。(例如“-”)。否则,我看不出它是一个
char*
,可能您的代码(从错误中提取)太简单了。分隔符是一个表示为字符串的唯一字符,因此(例如“-”、“d”)