0

Let's say I have a block of 512 bytes

void *block

How can I write for example in the area that starts with the 100nth byte and ends with the 150nth? How can I read from this area? I think that

void *memcpy(void *dest, const void * src, size_t n)

can't help me beacause with this call I can only take the firts n bytes. Any help is appreciated. Thanks in advance!

ryyker
  • 22,849
  • 3
  • 43
  • 87
PanD22
  • 79
  • 7
  • `void *block` is not a block of 512 bytes, it is a pointer variable, probably using from 2 to 4 bytes. And by the way, if you hover over the `block` tag, it will tell you : _DO NOT USE This tag is scheduled for removal because it can refer to many different things depending on the use of other tags with it._ – ryyker Dec 02 '20 at 15:24
  • A `char *` pointer can point to any byte. You can use it for direct byte access of any object. – 12431234123412341234123 Dec 02 '20 at 15:25
  • The `void` type (in terms of defining a variable.) cannot be used to _store_ values other than an address to another type. – ryyker Dec 02 '20 at 16:39

1 Answers1

0

"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:

  1. 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;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Thank you but I need block to be void *. I should use malloc to allocate 512 bytes and then write/read. – PanD22 Dec 02 '20 at 15:35
  • @PanD22 - [void * has limitations](https://stackoverflow.com/questions/339880/disadvantages-of-using-void-pointers-in-c), but I have modified to show that it can point to areas of populated memory, and print that area. – ryyker Dec 02 '20 at 15:57
  • yes I know but unfortunately I must use void *. Any solution with this limetation. – PanD22 Dec 02 '20 at 16:09
  • @PanD22 - `void *` cannot be used as an object type (as explained in edits to my post.), i.e. it cannot be used to store information of any type. Unlike other types however, it ***can*** be used to point to _any_ area of memory, no matter the type, `char`, `double`, `int` `struct`, etc. That is one of the unique qualities making `void` a very useful type. – ryyker Dec 02 '20 at 17:40