Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:43:35+00:00 2026-05-25T11:43:35+00:00

I have a sorted array of some indeterminate number of variables and counts thereof.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:43:36+00:00Added an answer on May 25, 2026 at 11:43 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some objects in an array and want to have them sorted. The
I have an array of objects and I need them sorted by their title
I have a sorted struct of IPs where I need to get the number
For this problem I have a sorted Array of doubles. I need to be
I have the following array that I need to have sorted from highest score
For some reason, I have a sorted php array: $arr_questions = Array [6] 0
I have a array like which is sorted by category Array ( [0] =>
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out
I have a sorted mutable array of a class called Topic. The topics represent
1-)For sorted array I have used Binary Search. We know that the worst case

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.