C 使用指针打印出值

C 使用指针打印出值,c,segmentation-fault,strcmp,C,Segmentation Fault,Strcmp,我有一个排序数组,其中包含一些不确定数量的变量及其计数。我需要像这样构建一个字符串: Attribute[0]: p=2, e=8 我的问题是数组实际上是一个指针,我不想使用固定长度的循环,所以我看到的唯一解决方案是使用数组指针 void printArray(Node_ptr node){ int count=0; //character count char *temp= node->attributes; //duplicate attribute array

我有一个排序数组,其中包含一些不确定数量的变量及其计数。我需要像这样构建一个字符串:

Attribute[0]: p=2, e=8
我的问题是数组实际上是一个指针,我不想使用固定长度的循环,所以我看到的唯一解决方案是使用数组指针

void printArray(Node_ptr node){
    int count=0; //character count
    char *temp= node->attributes; //duplicate attribute array

    char cur; //current char
    char *outputCat= emalloc(150); //concatenate counts to a single string

    strcpy(outputCat, "Attribute %d counts are: ");
    qsort(temp, lineCount, sizeof(char), compare); //sort the array

    while ((temp) != NULL){
        cur= *temp;
        ++temp;
        if (strcmp(&cur, temp)==0) //if characters are same
            count++; 
        else { //build reporting string
            strcat(outputCat, &cur);
            strcat(outputCat, "= ");
            sprintf(outputCat, "%d  ", count);
            count=0;
        }
    }
    free(outputCat);
}
这里的问题是strcmp(&cur,++temp)==0每次都返回false,即使我在调试器中看到它们的值。因此,else条件不断建立,并在多次迭代后抛出segfault

两个问题:

1-即使输入相同的值,
strcmp
也会返回非零值,这是什么原因造成的? 2-我能做些什么来修复代码?

在您的行中:

strcmp(&cur, temp)
cur
是本地声明的
char
,因此,
&cur
只是堆栈上的某个位置,在这种上下文中没有意义

我相信您的意思是检查当前字符
cur
是否与下一个字符
*temp

相同,如下所示:

if (cur == *temp) //if characters are same
    count++; 
void printArray(Node_ptr node){
    int count=0; //character count
    char *temp= node->attributes; //duplicate attribute array

    char cur; //current char
    char *outputCat= emalloc(150); //concatenate counts to a single string

    strcpy(outputCat, "Attribute %d counts are: ");
    qsort(temp, lineCount, sizeof(char), compare); //sort the array

    while (*temp != '\0'){
        cur= *temp;
        ++temp;
        if (cur == *temp) //if characters are same
            count++; 
        else { //build reporting string
            sprintf(outputCat, "%c = %d", *cur, count);  // e.g.   "e = 8"
            count=0;
        }
    }
    free(outputCat);
}
接下来,我将看一下“大规模简化您的输出”部分:

sprintf(outputCat, "%c = %d", *cur, count);  // e.g.   "e = 8"
count=0;
最后,我怀疑您的循环是否会终止,因为它会继续执行
temp++
,而
temp!=NULL

我相信您打算检查存储在指针
temp
上的值
*temp
应正确检查“\0”,而不是空值。
(\0和NULL恰好具有相同的值,但它们在语义上不应被视为相同)

p.S.您简单但出色的注释“//如果字符相同”对我理解您的代码非常有帮助。这是一个很好的例子,短而有意义的评论是非常宝贵的。多谢各位


(希望是最终编辑)
总之,我建议的更改如下所示:

if (cur == *temp) //if characters are same
    count++; 
void printArray(Node_ptr node){
    int count=0; //character count
    char *temp= node->attributes; //duplicate attribute array

    char cur; //current char
    char *outputCat= emalloc(150); //concatenate counts to a single string

    strcpy(outputCat, "Attribute %d counts are: ");
    qsort(temp, lineCount, sizeof(char), compare); //sort the array

    while (*temp != '\0'){
        cur= *temp;
        ++temp;
        if (cur == *temp) //if characters are same
            count++; 
        else { //build reporting string
            sprintf(outputCat, "%c = %d", *cur, count);  // e.g.   "e = 8"
            count=0;
        }
    }
    free(outputCat);
}

你觉得怎么样?

你不应该在
char
上使用
strcmp
strcat
。两个参数都必须是以NULL结尾的字符数组。这本身就可能导致一个故障。事实上,如果修复它可以完全修复你的代码,我也不会感到惊讶。我忘了字符可以直接比较而不需要函数。然而,改变这一点没有任何作用,因为比较总是被跳过,即使
cur
是“e”,正如
*temp
well seth一样,这就是答案,不是吗?你应该重新提交答案,以便Jason可以接受。更新你的代码,以便我们可以看到什么仍然不起作用。关键是'cur==*temp)行。现在,我要做一些更改,因为唯一数据点的数量因阵列而异。对于我的原始代码和这个代码,它适用于第一组值,但如果cur从e更改为p,则代码不会对其进行调整。