"How can I write for example in the area that starts with the 100nth byte and ends with the 150nth? "
First of all, regarding
void *block
Note that because void
is not a complete object type. it cannot be used to store data of any kind. Unlike other types however, a void *
can be used to reference any area of memory, no matter the type, char
, double
, int
, struct
, etc.
For example, this statement will fail with: ...error: array has incomplete element type 'void'
void buffer[2] = {'A', 'B'};
However the following will compile and work:
char buffer[2] = {'A', 'B'};
void *pBuffer = &buffer;//use void * to reference address of buffer
in ISO/IEC 9899:2017, §6.2.5 Types:
- The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.
(More on the void
keyword here.)
Create a block of memory as shown, initialize it to all NULL
, then make assignments to the areas of the block that you need to:
int main(void)
{
char memory_block[512] = {0};//define buffer with 512 bytes
void *block = NULL;
for(int i = 100;i < 149; i++)
{
*(memory_block + i) = 'A';//populate these locations with 'A'
}
//(or you could also use this syntax in the loop)
// memory_block[99] = 'A';
block = &memory_block[99]; //point block to specific area of memory containing data
printf("contents of location 99-149 - in block: %s\n", (char *)block);
//type cast void * to char *
return 0;
}