When you assign to a pointer, you need to dereference it, like this:
*pointer = i + 'a';
Instead of:
pointer = i + 'a';
Otherwise, all you’re doing is changing the value of the pointer itself, not the value of the memory location it is pointing to.
This explains your error: you’re trying to assign the value i + 'a'
(which is an integer) to a pointer type.
Another issue is that your malloc
call needs to allocate 27 characters, not 26, because you also need a terminating NUL
character.
Finally, when you go to print, your pointer has moved past the end of the string (because you’ve been incrementing it in your loop). The easiest way to fix this is to change pointer = i + 'a';
to pointer[i] = i + 'a';
and remove the pointer++
3
solved is it possible to set the value of allocated memory with loops?