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 8887647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:51:42+00:00 2026-06-14T21:51:42+00:00

I am asked to implement an analogue to the string.h library. However, I am

  • 0

I am asked to implement an analogue to the string.h library. However, I am not allowed to use the '\0' inside my library to end my string; that’s why I am supposed to use a structure for strings in my program.
I have this in mystring.h file:

#include <stdio.h>
#define MAXSTRLEN 100 // Defining the maximum lenght of the string as being 100

typedef struct scell *mystring_t;
mystring_t makemystring (char cs[]); // This function stores a given string into the mystring structure

I have this in mystring.cfile:

#include <stdlib.h>
#include "mystring.h" // including the header file of mystring library

struct scell {
    char *string;
    int length;
};

mystring_t makemystring (char cs[]){ //Storing a string into the structure

    int i = 0;
    mystring_t ns;

    ns->string=(char*)calloc(MAXSTRLEN ,sizeof(char));

    // printf ("I allocated memory for the string");

    while (cs[i] != '\0')
    {
        printf ("\nI entered into the while\n");
        ns->string[i] = cs[i];
        printf ("I inserted\n");
        i++;
        printf ("I incremented the count\n");
    }
    ns->length=i; // storing the length of the string into the structure
    printf ("%d\n", ns->length);
    printf ("refreshed the length\n");
    printf ("%d", ns->length);
    return ns;
}

I have this in the main.c file:

#include "mystring.h"
#include <stdlib.h>
int main () {

int result;
mystring_t S1;
mystring_t S2;
    // create two strings
S2 = makemystring("Bye");
printf("I got out of the makemystring function\n");
S1 = makemystring("Hi");

These printf() calls are just debugging statements.
It seems to me that the function makemystring works correctly but I have a crash at the level of returning.
Can anyone please help?

  • 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-14T21:51:43+00:00Added an answer on June 14, 2026 at 9:51 pm

    ns is an uninitialized pointer when it is dereferenced:

    mystring_t ns;
    
    ns->string = (char*)calloc(MAXSTRLEN ,sizeof(char));
    

    as mystring_t is a typedef for struct cell*. Allocate memory for ns before using:

    mystring_t ns = malloc(sizeof(*ns)); /* No cast on return value required. */
    if (ns)
    {
        ns->string = calloc(MAXSTRLEN, 1);
        ns->length = 0;
    }
    

    FWIW, this is one of the reasons I dislike hiding pointers in typedefs as it is not obvious at point of use.

    Protect going beyond the bounds of ns->string in the condition of the while loop.

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

Sidebar

Related Questions

I've been asked to implement a security requirement that we instruct browsers not to
I was asked to implement a menu bar that is neither horizontal nor vertical.
Today at school the teacher asked us to implement a duplicate-deletion algorithm. It's not
At a recent job interview, I was asked to implement my own string copy
I've been asked to implement some code that will update a row in a
I asked here about a protocol that I was asked to implement, and how
We have been asked to implement a Red Black Tree in java, but not
I have been asked to implement some email address validation on a web app
I was asked to implement a code to copy some info from a Oracle
This is the gallery I've been asked to implement http://sandbox.leigeber.com/slideshow/ I've chopped and changed

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.