Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP T_T This is long but the question should be pretty straight forward

ID: 3625413 • Letter: P

Question

PLEASE HELP T_T
This is long but the question should be pretty straight forward : WHY ARE ANY CHARACTERS I PRINT OUT IN SORT HISTOGRAM NULL?!

basically what this is supposed to do is irrelevant, all i need to know is why my printLookup works great and prints out all the right values, but by the time it gets to sort, everything i try to print out there is null >.< ...

struct char_info *histogram ()
{
    char c;
    struct char_info *np = NULL;
    struct char_info *head = NULL;
    c = getchar();
    while(c!=EOF)
    {
         if(head==NULL)
         {
                 head = (struct char_info *)malloc(sizeof(struct char_info *));
                 head->ch = c;
                 head->count = 1;
                 head->next = NULL;
         }
        else
        {
               np = lookup(c, head);
               if(np==NULL)
               {
                      np = (struct char_info *)malloc(sizeof(struct char_info *));
                      np->next = head;
                      np->ch = c;
                      np->count = 1;
                      head = np;
               }
               else
                     np->count++;
         }
        c = getchar();
    }

printLookup(head);
sortHistogram(head);
/* some other stuff */
}


/*this works perfectly*/

void printLookup (struct char_info *head)
{
         struct char_info *np;
         for(np = head; np!=NULL; np = np->next)
         {
                printf("Char: %x, Count: %d ", np->ch, np->count);
         }
}

/*sorts head by putting sorted elems into static list*/
void sortHistogram (struct char_info *head)
{
      struct char_info *np;
      np = head;
     printf("character!!!!: %c ", np->ch); /*nothing in ch / null / nothing prints after "character!!!!: "*/
     while ( head != NULL )
     {
           add(head);
           printf("character being inserted: %c ", np->ch);
           printf("character in sortHist: %c ", sortedHist->ch);
           head = head->next;
      }
}

so for some reason, when i print out np->ch and or any of the other printfs in sortHistogram, i get NOTHING. printLookup gives me beautiful, perfect counts, but as soon as I call sortHistogram for some reason, everything is just null. NULLS EVERYWHERE. Any idea what I should be looking out for or why head is NOT null at first but it is giving me a char that is null?
I'd really appreciate it, I've been stuck for a while. Thanks! I will give lifesaver rating to anyone who gives a good answer!

Explanation / Answer

How do i delete a question?!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote