Why this code isn't working?
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char **argv){
char * new;
new = malloc(10);
const char * s1 = "hello";
while(*s1){
*new++ = *s1++;
}
printf("%s",new);
return 0;
}
I ran the gdb and found that *s1 is not assigned to the *new. why?
newin your loop, so it no longer points to the beginning of the block you allocated -- it points to the character after the last one you copied in. You also haven't copied the null terminator.