Question
This is a program that overwrites an input string with 232 is awesome. However, there are some bugs in the code. Please read the code
This is a program that overwrites an input string with 232 is awesome. However, there are some bugs in the code. Please read the code and fix these bugs. An expected output of working replace_str is like below:
% ./replace_str
% replaced string = 232 is awesome!
#include
void U(int n); void A(void); void val(int x); void addr(int *x); // ---------------------------------------------------------------------------- // // ------------------------------ Global Variables ---------------------------- // // ----------------------------------------------------------------------------
int m = 1; // This is a global variable
// ---------------------------------------------------------------------------- // // ------------------------------- Main Program ------------------------------- // // ---------------------------------------------------------------------------- int main(void) {
// --------------------------------------------------------------------- // Blocks: // --------------------------------------------------------------------- // Any code may be surrounded by braces { ... } // Such code represents a block, which may have its own local variables.
int n = 7; printf("main : m=%d, n=%d ", m, n); U(n); printf("main : m=%d, n=%d ", m, n); A(); printf("main : m=%d, n=%d ", m, n);
// ------------------------------------------------- // Below is a block, which may declare its own local // variables: // ------------------------------------------------- { // ------------------------------------------------- // A local variable n is declared here. // References to m refer to the global m. // ------------------------------------------------- int n = 100;
printf("block : m=%d, n=%d ", m, n); U(n); printf("block : m=%d, n=%d ", m, n); // ------------------------------------------------- // A local variable m is now declared. // References to m from here to the end of the // block refer to this m. // ------------------------------------------------- int m = -40;
printf("block : m=%d, n=%d ", m, n); val(m); printf("block : m=%d ", m); addr(&m); printf("block : m=%d ", m);
}
printf("main : m=%d, n=%d ", m, n); // // --------------------------------------------------------------------- return 0; }
// ---------------------------------------------------------------------------- // // -------------------------- Subprogram Definitions -------------------------- // // ----------------------------------------------------------------------------
void U(int n) { m = n; printf(" U: m=%d, n=%d ", m, n); }
// ---------------------------------------------------------------------------- // void A(void) { int m = 3; { extern int m; m = 5; printf(" global m=%d ", m); } printf(" A: m=%d ", m); } // ----------------------------------------------------------------------------
void val(int x) { printf("val : x=%d ", x); x = 5; } // ---------------------------------------------------------------------------- // -------------can't help testing your pass-by-pointers -Jun ----------------- void addr(int *x) { printf("ref : *x=%d ", *x); *x = 5; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started