我在这里有一些奇怪的输出。你能解释我为什么以及如何解决它?C字符串malloc(输出)
int inp_str(char * string, char ** pointers[])
{
char * tmp[stringsCount];
if (strlen(string) > maxlen)
return (-1);
else {
tmp[count] = malloc(sizeof(char) * strlen(string));
strcpy(tmp[count], string);
pointers[count] = &tmp[count];
count++;
}
return count;
}
int main(){
//char * strings[stringsCount];
char ** pointers[stringsCount];
inp_str("sdasya", pointers);
inp_str("dasd", pointers);
inp_str("qwe", pointers);
inp_str("dasd", pointers);
//sort(pointers, count);
printf("%s", *pointers[0]);
printf("\n%s", *pointers[1]);
printf("\n%s", *pointers[2]);
printf("\n%s", *pointers[3]);
}
这里是输出:
sdasya
��uNH��H�l$ H�\$L�d$(L�l$0H��8�f.�
qwe
�bs7
PS。 stringsCount是常量; count = 0
欢迎来到Stack Overflow。请尽快阅读[关于]页面。您应该了解如何创建SSCCE([简短,独立,正确的示例](http://sscce.org/))。你的代码与SSCE相当接近,但它看起来只需要四行代码即可完成:两条'#include'行和'stringsCount'和'count'的定义。包括那些将会节省你编写最后一条PS线。 –