Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用printf()添加不需要的空格/行的格式化输出-C编程_C_String_Formatting_Printf_String Formatting - Fatal编程技术网

使用printf()添加不需要的空格/行的格式化输出-C编程

使用printf()添加不需要的空格/行的格式化输出-C编程,c,string,formatting,printf,string-formatting,C,String,Formatting,Printf,String Formatting,这就是我遇到的问题所在的函数。最后主要是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 < ta

这就是我遇到的问题所在的函数。最后主要是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);
    }
}
这之后呢

memset(thisName,0,sizeof(thisName));
%s
需要以null结尾的字符串。或者在退出循环之后

thisName[size] = '\0';
编辑:

数组大小应足够大,以容纳字符串的空终止

char thisName[size + 1];
字符*name,*ptr

ptr=name=&(标记计数器[ndx]->标记名[0])

而(*ptr!='\0') ptr++

大小=ptr-名称

在printf之前:


此名称[大小]='\0'

谢谢,我不知道你能用那样的PTR做算术。格式化问题是因为我的字符串有空格吗?你的变量“thisName”没有字符串终止符(NULL),在屏幕上打印垃圾也是如此。这很有意义。我在末尾添加了terminate字符,但它并没有解决问题。就像我说的,我是C新手,我有java经验,但尽管他们很亲近,但他们就像是在拿狮子和斑马做比较。“我不熟悉memset,我会调查一下的。”乔治。问题是您没有空间来存储空字符。将数组大小设置为
size+1
这并没有缓解问题,不幸的是。memset解决了我稍后遇到的另一个问题!再加上添加了空指针,再加上一些研究/对C和编程有了更好的理解,总体上修复了我的程序。谢谢你的帮助!
thisName[size] = '\0';
char thisName[size + 1];