I am new to C and want to know why this program adds garbage at the end of the string.
#include <stdio.h>
#include <stdlib.h>
char* cpy(char*);
int main() {
char *old = "Hello All Now i thought this would work apparently";
char *newer = cpy(old);
printf("%s",newer);
getchar();
return 1;
}
char* cpy(char* old) {
char* newer = (char*)malloc(sizeof(old)-1) ;
int i = 0;
while(old != '\0')
newer = old[i++];
return newer;
}
#include <stdio.h>
#include <stdlib.h>
char* cpy(char*);
int main() {
char *old = "Hello All Now i thought this would work apparently";
char *newer = cpy(old);
printf("%s",newer);
getchar();
return 1;
}
char* cpy(char* old) {
char* newer = (char*)malloc(sizeof(old)-1) ;
int i = 0;
while(old != '\0')
newer = old[i++];
return newer;
}