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

  • SEARCH
  • Home
  • 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 8244185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:45:53+00:00 2026-06-07T21:45:53+00:00

I’m trying to write a simple C code for the following function: Given an

  • 0

I’m trying to write a simple C code for the following function:

Given an encoding for the letters a-d:

‘a’->00, ‘b’->01, ‘c’->10, ‘d’->11,

and a node in a linked-list defined as:

typedef struct listNode{
 unsigned char data;
 struct listNode* next;
}ListNode;

typedef struct list{
  ListNode* head;
  ListNode* tail;
}List;

where head points to the first node of the list, and the tail to the last one.

I need to write a function char* SumList(List arr[], int n), which returns a string which contains all the encoded letters in all the nodes of all lists in arr in row.

This is what I wrote so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int isBitISet(char, int);

typedef struct listNode {
    unsigned char data;
    struct listNode* next;
} ListNode;

typedef struct list {
    ListNode* head;
    ListNode* tail;
} List;

int isBitISet(char ch, int i) {

    char mask;
    mask=1<<i;
    return (mask & ch);
}

int totalNodes(List arr[], int n) {
    int i;
    int counter=0;

    for (i=0; i<n; ++i) {
        ListNode* head= arr[i].head;
        while (head!=NULL) {
            counter++;
            head=head->next;
        }
    }
    return counter;
}

char* whatToadd(char data) {
    int a, b;
    a=isBitISet(data, 0);
    b=isBitISet(data, 1);
    char* result;
    result=(char *) calloc(2, sizeof(char));
    if ((a!=0) && (b!=0))
        result="d";
    else if ((a!=0) && (b==0))
        result="b";
    else if ((a==0) && (b!=0))
        result="c";
    else
        result="a";
    return result;

}

char* SumLists(List arr[], int n) {

    char* final;
    int nodes=totalNodes(arr, n);
    final= (char*) calloc(nodes, sizeof(char)); //how would I know the final length?//
    int i;

    for (i=0; i<n; ++i) {
        ListNode* head= (arr[i].head);
        while (head!=NULL) { //Why do I need a tail?//
            char* result;
            result=whatToadd(((head->data)&(00000011)));
            strcat(final, result);
            free(result);
            result=whatToadd(((head->data)&(00001100))>>2);
            strcat(final, result);
            free(result);
            result =whatToadd(((head->data)&(00110000))>>4);
            strcat(final,result);
            free(result);
            result=whatToadd(((head->data)&(11000000))>>6);
            strcat(final,result);
            free(result);

            head=head->next;

        }
    }
    return final;
}

int main() {
    .....

    free(final);
    ...
}

Probably, Tail has given from some reason- but (1)– can’t I run on the lists the way I did? without using the tail? and if not, how should I use it?

(2)– Do I need to free result the way I did each time I get a new result from whatToAdd?

I’m new to C, trying to work it by myself, I would really appricate tips and corrections. Thanks a lot.

  • 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-06-07T21:45:55+00:00Added an answer on June 7, 2026 at 9:45 pm

    Do I need to free result the way I did each time I get a new result from whatToAdd?

    No, as the address of a string literal is being returned. Don’t calloc() memory for result as it is unrequired. Change the return value of whatToadd() to const char*, as string literals are read-only.

    Just to clarify, the following does not copy "d" into result:

    result="d";
    

    but instead makes result point to the address of the string literal "a": it is a pointer assignment. If you wanted to copy into result you could do either:

    • strcpy(result, "d");
    • *result = 'd'; *(result + 1) = 0;

    in which case you would free() the returned pointer.

    Only pass a pointer to free() if it is the result of a previous call to calloc(), malloc() or realloc() and has not already been free()d.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.