使用指针打印值

发布于 2024-12-04 05:15:58 字数 1117 浏览 3 评论 0原文

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

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 条件会不断构建,并在多次迭代后抛出段错误。

两个问题:

1- 即使输入相同的值,什么才能使 strcmp 返回非零? 2- 我可以做什么来修复代码?

I have a sorted array of some indeterminate number of variables and counts thereof. I need to build a string like so:

Attribute[0]: p=2, e=8

My issue is the array is actually a pointer, and I don't want to use a loop of fixed length, so the only solution I see is to use an array pointer.

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);
}

The issue here is strcmp(&cur, ++temp)==0 is returning false every time, even when I see their values in the debugger. Because of this, the else conditional is constantly being built up and throws a segfault after a number of iterations.

Two questions:

1- What can make strcmp return non-zero even when identical values are entered?
2- What can I do to fix the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2024-12-11 05:15:58

在您的行中:

strcmp(&cur, temp)

cur 是本地声明的 char,因此,&cur 只是堆栈上的某个位置,在这个背景。

我相信您的意思是检查当前字符 cur 是否与下一个字符 *temp 相同。
这看起来像:

if (cur == *temp) //if characters are same
    count++; 

接下来,我会看看大规模简化你的输出部分:

sprintf(outputCat, "%c = %d", *cur, count);  // e.g.   "e = 8"
count=0;

最后,我怀疑你的循环是否会终止,因为它继续执行 temp++,而 temp != NULL
我相信你打算检查存储在指针处的 VALUE 临时
应根据“\0”而不是 NULL 正确检查 *temp
(\0 和 NULL 碰巧具有相同的值,但它们在语义上不应该被视为相同)

while (*temp != '\0'){

PS 您的简单但出色的评论“// if strings are same “对我理解你的代码非常有帮助。这是一个简短而有意义的评论无价值的绝佳案例。谢谢。


(希望是最终编辑)
总的来说,我建议的改变是这样的:

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);
}

你觉得效果如何?

In your line:

strcmp(&cur, temp)

cur is a char declared locally, and therefore, &cur is just some location on the stack, and rather meaningless in this context.

I believe you mean to check if the current-character cur is the same as the next character *temp.
This would look like:

if (cur == *temp) //if characters are same
    count++; 

Next, I would look at massively simplify your output section:

sprintf(outputCat, "%c = %d", *cur, count);  // e.g.   "e = 8"
count=0;

And finally, I doubt your loop will ever terminate, as it continues to do temp++, while temp != NULL.
I believe you intend to check the VALUE stored at the pointer temp.
*temp should be properly checked against '\0', instead of NULL.
(\0 and NULL happen to have the same value, but they should not be treated the same semantically)

while (*temp != '\0'){

P.S. Your simple, but excellent comment "// if characters are same" was extremely helpful for me to understand your code. This is an excellent case of short, meaningful comments being INVALUABLE. Thank you.


(Hopefully final edit)
In total, the changes I recommend look like:

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);
}

How's that work out for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文