gpt4 book ai didi

c - Strtok 使用动态内存

转载 作者:太空宇宙 更新时间:2023-11-04 02:43:56 25 4
gpt4 key购买 nike

我有以下代码,我需要帮助获取和存储最后一个 token 。现在代码在每个空格后标记化,但是当它到达我的文本文件的末尾时,它不会标记最后一个值。我很确定我需要在 malloc 语句之前拥有 token ,但是当我在前面添加它时,我遇到了段错误。有人知道这个问题吗?由于 realloc 而初始化了上面的 myStruct.extras = NULL;它是一个 char **

token = strtok(fileArrayPTR[p],"X");
while (token!= NULL)
{

if (tempCounter == 0)
{
token = strtok(NULL," ");
myStruct.dimensions[1] = strtol(token,&ptr,10);
}else{
myStruct.extras = realloc(myStruct.extras,(extraCounter + 1) * sizeof(char *));
myStruct.extras[extraCounter] = malloc(strlen(token)+1);
strcpy(myStruct.extras[extraCounter],token);
token = strtok(NULL," ");
extraCounter++;
}
}

编辑:忘了放递增的计数器

最佳答案

这是问题的(第二个版本)代码:

token = strtok(fileArrayPTR[p],"X");
while (token!= NULL)
{
if (tempCounter == 0)
{
token = strtok(NULL," ");
myStruct.dimensions[1] = strtol(token,&ptr,10);
}else{
myStruct.extras = realloc(myStruct.extras,(extraCounter + 1) * sizeof(char *));
myStruct.extras[extraCounter] = malloc(strlen(token)+1);
strcpy(myStruct.extras[extraCounter],token);
token = strtok(NULL," ");
extraCounter++;
}
}

这不是一个独立的程序。我们可以通过以下方式改进它:

  • 删除结构。
  • 显示相应的变量声明。
  • 将其转换为简单的 main()

这些变化导致:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
int extraCounter = 0;
char **extras = 0;
int tempCounter = 0;
char *ptr = 0;
char data[] = "10X16 something another-thing";
char *token = strtok(data, "X");
int dimension1 = strtol(token, &ptr, 10);
int dimension2 = -1;
while (token!= NULL)
{
if (tempCounter == 0)
{
token = strtok(NULL," ");
dimension2 = strtol(token, &ptr, 10);
tempCounter++;
}
else
{
extras = realloc(extras, (extraCounter + 1) * sizeof(char *));
extras[extraCounter] = malloc(strlen(token)+1);
strcpy(extras[extraCounter], token);
token = strtok(NULL, " ");
extraCounter++;
}
}

printf("Dimensions: %dx%d\n", dimension1, dimension2);
for (int i = 0; i < extraCounter; i++)
printf("%d: [[%s]]\n", i, extras[i]);

return 0;
}

运行时,它会产生:

Dimensions: 10x16
0: [[16]]
1: [[something]]
2: [[another-thing]]

代码有问题吗?是的,dimension2 的代码不会重新调用 strtok(),因此 16 被处理了两次,一次作为维度,一次作为字符串。可能不是想要的。因此:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
int extraCounter = 0;
char **extras = 0;
int tempCounter = 0;
char *ptr = 0;
char data[] = "10X16 something another-thing";
char *token = strtok(data, "X");
int dimension1 = strtol(token, &ptr, 10);
int dimension2 = -1;
while (token != NULL)
{
if (tempCounter == 0)
{
token = strtok(NULL, " ");
dimension2 = strtol(token, &ptr, 10);
tempCounter++;
}
else
{
extras = realloc(extras, (extraCounter + 1) * sizeof(char *));
extras[extraCounter] = malloc(strlen(token) + 1);
strcpy(extras[extraCounter++], token);
}
token = strtok(NULL, " ");
}

printf("Dimensions: %dx%d\n", dimension1, dimension2);
for (int i = 0; i < extraCounter; i++)
printf("%d: [[%s]]\n", i, extras[i]);

return 0;
}

编译:

gcc -g -O3 -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Werror ss.c -o ss

输出:

Dimensions: 10x16
0: [[something]]
1: [[another-thing]]

看起来更好...

关于c - Strtok 使用动态内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com