You say you think of data
as an array, therefore it is better that you declare data
as an array instead of a pointer. (The way your code is now you have a pointer that is initialized incorrectly by casting the character 'a'
to a char *
pointer. That way it will not be pointing anywhere.)
You can replace characters by assigning to elements of the array, and you can shift parts of the data in the array using memmove
.
Which means that maybe you want something like this:
unsigned char data[] = {'a','b','c','d','e'};
data[0] = 'R';
memmove(data + 1, data + 3, sizeof(data) - 3);
The memmove
call moves sizeof(data) - 3
bytes of data from address data + 3
to address data + 1
. The function memmove
even works when the regions of memory between which you are moving bytes of data overlap.
If you then print the relevant part of your data
:
fwrite(data, 1, sizeof(data) - 2, stdout);
putchar('\n');
This will get you the output:
Rde
However, notice that the size of the array will not have changed. It still will be five characters long. So replacing abc
by something longer than three characters will not work like this. Also, this array is not a null-terminated string, which is the more usual way to have sequences of characters in C.
If you prefer to use a string "abcde"
instead of what you are doing now (but then why call it "data"?), add a comment below this answer, and I'll extend it.