-
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 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); }
Output:
-
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 crashchar 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);
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}