这是我遇到问题的函数。主要是最后的 printf 语句。我对 C 比较陌生,但我觉得这是一个不寻常的结果。非常感激任何的帮助!
void frequencies(char htmlDoc[]) {
tags(htmlDoc, 0);
int ndx;
// struct tagCounter *pCursor = tagCounters[0];
int size = 1;
char *name;
for (ndx = 0; ndx < tagCntSize; ndx++) {
name = &(tagCounters[ndx]->tagName[0]);
while (*(name + 1) != '\0') {
size++;
name++;
}
name = &(tagCounters[ndx]->tagName[0]);
char thisName[size];
int index;
int thisCount = tagCounters[ndx]->tagCount;
for (index = 0; index < size; index++) {
thisName[index] = *name;
name++;
}
printf("%s\t%i\n", thisName, thisCount);
}
}
这是结果输出:
img
� 1
img
1
img
1
img
1
img
1
img
1
Ready
最佳答案
char thisName[size];
在此之后执行
memset(thisName,0,sizeof(thisName));
%s
需要一个以 null 结尾的字符串。或者退出循环后
thisName[size] = '\0';
编辑:
数组大小应足够大以容纳字符串的空终止符。
char thisName[size + 1];
关于c - 使用 printf() 添加不需要的空格/行的格式化输出 - C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29487768/