程序应根据输入创建字符串。我们想将这些字符串添加到一个列表中,该列表作为指针传递给函数(指向其他 char * 指针)。代码如下所示:
void main(void) {
//set angles alpha (small) & beta (large)
char ** configurations = calloc(0, sizeof(char*));
int multiplicity = 0;
createConfigString(4, 4, 0, configurations, 0, &multiplicity);
}
void createConfigString(int a, int b, int c, char ** configurations, int start, int * multiplicity) {
int x, i;
int strSize = 2 * (a + b + c);
for(x = a; x >= (a + (a % 2)) / 2; x--) {
//new entry to configurations if starting a new line
if(start == 0) {
configurations = realloc(configurations, (*multiplicity + 1) * sizeof(char *));
configurations[*multiplicity] = calloc(strSize, sizeof(char));
}
for(i = 0; i < x; i++) {
configurations[*multiplicity][start] = "a,";
}
if(b == 2) {
*multiplicity++;
configurations[*multiplicity][start + 2 * x] = "b,b\n";
start = 0;
continue;
}
configurations[*multiplicity][start + 2 * x] = "b,b,";
createConfigString(a - x, b - 2, c, configurations, start + 2 * x + 4, multiplicity);
}
}
但是在编译时它告诉我们它正在尝试将指针转换为 int 行
configurations[*multiplicity][start] = "a,";
configurations[*multiplicity][start + 2 * x] = "b,b\n";
configurations[*multiplicity][start + 2 * x] = "b,b,";
当我们写
configurations[*multiplicity][start] = (int) "a,";
等等它确实在没有任何警告的情况下编译
我们做错了什么?谢谢