const void* elem1 means that elem1 is a pointer to something const (as opposed to a const pointer). If you reinterpret that as an array of char pointers to something const, you are casting off the const-ness of elem1. You have to say that the contents of the array are const.

By trying the cast from <const void *> to <const char **> you are trying to cast away the constness since it is the value pointed to be the first dereference of elem1 that is const. Instead you should be casting to <char * const *>. Also you should probably use static_cast, since the semantics of this cast are more well defined and guaranteed to be portable (although in this case reinterpret_cast is probably equivalent).

reinterpret_cast - internally interpret the pointers ( source pointer and target pointer ) and while compile time it embeds extra code if neccessary.

Here your treatment on pointers is converting from void* to char **. Which are not compatable

You are trying to copy only the 4 bytes of pointer data ( ex. 0x023FD2F) to the target pointer.

const char **p1 = reinterpret_cast< const char**>(elem1); - this casting should not be used here

reinterpret_cast is used when some situations like virtual pointer hirerchy casting
For only pointer copy it is not neccessary an extra interpretation is neccessary by the compiler then this casting is supportable

void* is a strange beast. It is a pointer to something unspecified. There is nothing wrong with saying that the unspecified thing is a char**, in fact we'd be lost if we weren't able to typecast void* in thread code.

You can do the following with a static_cast per the standard. I was surprised that the standard does not guarantee reinterpret_cast to work for void* too, but it does indeed say that it is implementation specific. Neither static nor reinterpret casts cast off constness though.

---------8<--------
#include <cstdio>
#include <memory>

int main()
{
// malloc() - more comfortable in C code, but
// what the hell. It is a simple example of
// something that returns a void*, when you
// really mean it to return a pointer to something
// different
void *ptr = malloc(sizeof(char*) * 2);

// However, we were allocating an array of 2 char* s
char **arr = static_cast<char**>(ptr);

// Just to show it works
arr[0] = "one";
arr[1] = "two";
for (int i = 0;i < 2;i++)
printf("String %d is \"%s\"\n",i+1,arr[i]);

free(ptr);
}
---------8<--------

NB: it looks odd written like this, because you are more used to seeing a C cast in C-ish C++.

char** arr = (char**)malloc(sizeof(char*) * 2);

0

Featured Post

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…

The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.