我想编写一个处理如下所示字符串的函数:
| 1,2,3,4 |(1→2),(2→3),(3→1)|
结果应该是将字符串分解为这些字符串:
- 1
- 2
- 3
- 4
- (1→2)
- (2→3)
- (3→2)
这是我的代码:
int processPart(char*** dest, char* from) //Processes a half at a time
{
int len = 0;
char* cutout = strtok(from, ",");
while(cutout)
{
(*dest) = (char**)realloc(dest, (len + 1) * sizeof(char*)); <<<<<<<
(*dest)[len] = (char*)calloc(strlen(cutout) + 1, sizeof(char));
memcpy((*dest)[len], cutout, strlen(cutout));
cutout = strtok(NULL, ",");
len++;
}
return len;
}
void processInput(char*** vertices, char*** edges, char* input, int* sizev, int* sizee)
{
int vlen = 0, elen = 0;
char* string = input + 1;
char* raw_vertices;
char* raw_edges;
string[strlen(string)] = '\0';
raw_vertices = strtok(string, "|");
raw_edges = strtok(NULL, "|");
*sizev = processPart(vertices, raw_vertices); //First the vertices
*sizee = processPart(edges, raw_edges); //Then the edges
}
int main()
{
char* in = stInput(); //input function
char** c = NULL, **b = NULL;
int a, d, i;
processInput(&c, &b, in, &a, &d);
for(i = 0; i < a; i++)
{
printf("%s\n", c[i]);
}
printf("++++++++++++++++");
for(i = 0; i < d; i++)
{
printf("%s\n", b[i]);
}
return 0;
}
但是,我在标记为&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
任何人都知道我的错误是什么?
答案 0 :(得分:1)
在错误的行中
(*dest) = (char**)realloc(dest, (len + 1) * sizeof(char*));
在*
参数之前缺少dest
。如果你没有用无用的演员表达混乱的表情,你可能会发现这更容易。我写了
*dest = realloc(*dest, (len + 1) * sizeof**dest);
- 这样我们可以更好地看到赋值的第一个参数和左操作数的匹配。