-1
我的代码的这一部分是关于注册。如何在动态二维数组中保存字符串?
我只能注册一次,下一次程序停止。
什么问题?
while (1) { /*usercounter initialized with 0*/
printf("enter your order:\n");
gets(buffer);
order = strtok(buffer, " ");
if (strcmp(order, "signup") == 0) {
usercounter++;
if (usercounter > 50) {
username=realloc(username,usercounter*sizeof(*username));
password=realloc(password, usercounter*sizeof(*password));
}
username[(usercounter - 1)] = (char *)malloc(50*sizeof(char));
strcpy(username[usercounter - 1], strtok(NULL, " "));
password[(usercounter - 1)] = (char *)malloc(50*sizeof(char));
strcpy(password[usercounter - 1], strtok(NULL, "\n"));
free(buffer);
continue;
}
}
[不要使用'gets()',这是危险的](http://stackoverflow.com/q/1694036/2173917)。改用['fgets()'](https://linux.die.net/man/3/fgets)。 –
'免费(缓冲区);'!? – BLUEPIXY
我在这里看到的是非常危险的代码和漏洞。我没有足够精通C语言来解决修复问题,而没有引入其他错误,但在free,gets()问题后有一个用法,而且我看不到任何管理(计数)引用的方式,因此泄漏和错误很可能会发生。 – gelliott181