I'm trying to do very basic examples to understand how void pointers work. Here's an example I've written for having a void*
string and casting it to its "working" type and printing some aspects of it:
int main(int argc, char *argv[]) {
// Create a void pointer which "acts like" a string
void * string = "hello";
// "Cast" the string so it's easier to work with
char * string_cast = (char*) string;
// Print the string and a character in it
printf("The string is: %s\n", string_cast);
printf("The third character is: %c\n", string_cast[2]);
// How to now do something like:
// (1) void pointer_to_string_obj = ?
// (2) cast that pointer_to_string_obj to a normal string
// (3) print the string like it would normally be done
}
Could someone please show an example of manually creating a string pointer of type *(char**)
and why that type would need to be created in the first place (why not just a normal char*
?). I apologize if my question is broad, but basically I'm trying to figure out various void pointer types and where I'm at now in my very beginner understanding, it's a bit confusing, and so seeing a few examples would be very helpful.