Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic memory allocation in a multiple-files project in C
    primarykey
    data
    text
    <p>I am asked to implement an analogue to the <code>string.h</code> library. However, I am not allowed to use the <code>'\0'</code> 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 <code>mystring.h</code> file:</p> <pre><code>#include &lt;stdio.h&gt; #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 </code></pre> <p>I have this in <code>mystring.c</code>file:</p> <pre><code>#include &lt;stdlib.h&gt; #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-&gt;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-&gt;string[i] = cs[i]; printf ("I inserted\n"); i++; printf ("I incremented the count\n"); } ns-&gt;length=i; // storing the length of the string into the structure printf ("%d\n", ns-&gt;length); printf ("refreshed the length\n"); printf ("%d", ns-&gt;length); return ns; } </code></pre> <p>I have this in the <code>main.c</code> file:</p> <pre><code>#include "mystring.h" #include &lt;stdlib.h&gt; 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"); </code></pre> <p>These <code>printf()</code> 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload