I have this c code which is supposed to print a line of textbut it has some logi
ID: 3613717 • Letter: I
Question
I have this c code which is supposed to print a line of textbut it has some logic problems and memory related problems. Cananybody please look through it and modify the code for me.#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//
// A short program to print the string "Hello, Fred"
//
int main (int argc, const char *argv[])
{
char name[5];
char ch = name[5];
char *last_char = &name[5];
ch = *last_char;
char *src = "Fred";
char *dst;
// copy the name
while (++src)
{
*dst = *src;
}
char *msg = (char *) malloc (5);
strcpy (msg, "Hello, ");
strcat (msg, name);
free (last_char);
free (msg);
*last_char = 'A';
free (msg);
msg = (char *) malloc (5);
msg = src;
puts (msg);
return 0;
}
Explanation / Answer
x.