What do you mean by “not working”, can you be more specific?
Two observations:
Where do d and dest point after the loop?
In your first example you loose the address of the initially allocated memory block. After the loop there’s no way to free the memory so you have a leak.
Oh, and you need to increment sour in the first example, not s.
Okay, yes, I’d expect that. After the loop the char pointers you’re trying to print point to one position after the string. You need to somehow save the start of the block. I’d strongly advise you not to do this kind of pointer arithmetic with dynamic memory anyway. You’re better off with something like this:
void print_array(const char* src) {
char* dest = malloc(100 * sizeof(char));
int i = 0;
while (src[i] != '\0' && i < 100) {
dest[i] = src[i];
++i;
}
printf("%s\n", dest);
free(dest);
}