Move History

Fork Selected
  • Description

    To make Hello World return string modifiable, can always allocate memory for the string. Here the code should produce "Xello World." when return string modified.

    Code
    char* Hi (void)
    {
      char* ans=malloc(13);
      strcpy(ans,"Hello World.");
      return ans;
    }
    
    Test Cases
    #include <criterion/criterion.h>
    const char* Hi ();
    
    Test(the_hello_world_function, should_pass_all_the_tests_provided) {
        cr_assert_eq(strcmp(Hi(),"Hello World."), 0);   
    
        char *p  = Hi();
        *p = 'X';
        printf("%s\n",p);
    }
  • Code
    • const char* Hi (void)
    • char* Hi (void)
    • {
    • return("Hello World.");
    • }
    • char *oopsHi(void)
    • {
    • return ("Hello World.");
    • char* ans=malloc(13);
    • strcpy(ans,"Hello World.");
    • return ans;
    • }
    Test Cases
    • // TODO: Replace examples and use TDD development by writing your own tests. The code provided here is just a how-to example.
    • #include <criterion/criterion.h>
    • // replace with the actual method being tested
    • const char* Hi ();
    • Test(the_multiply_function, should_pass_all_the_tests_provided) {
    • cr_assert_eq(Hi(""), "Hello World.");
    • #if 0 // if'd out, reenable to observe crash
    • char oopsHi();
    • char *p = oopsHi();
    • Test(the_hello_world_function, should_pass_all_the_tests_provided) {
    • cr_assert_eq(strcmp(Hi(),"Hello World."), 0);
    • char *p = Hi();
    • *p = 'X';
    • #endif
    • printf("%s\n",p);
    • }