hi
i want to make a function that will take for input a number n and a character b
the function will return a string that contains n times the character b
i tried this but it doesnt work
after some thinking i tried dynamic memory allocationCode:char *s(int n, char b) { int i; char r[n]; for (i=0;i<n;i++){ r[i] = b; } return r; }
and it workedCode:char *s(int n, char b) { int i; char *r; r = (char *)malloc(n*sizeof(char)); for (i=0;i<n;i++){ r[i] = b; } return r; }
can anyone tell me why?
ty