The task in my school is as follows:
Create a function which will take as parameter ******str
pointer to pointer to pointer to pointer to pointer to pointer of char
and sets Follow the white rabbit!
to the pointer of char.
void mx_deref_pointer(char ******str);
I am new to C and am extremely confused, even though I've learnt everything I could find about pointers..(
I have come up with the following code:
#include <stdio.h>
#include <stddef.h>
void mx_deref_pointer(char ******str) {
char *pstr1, **pstr2, ***pstr3, ****pstr4, *****pstr5;
str = &pstr5;
pstr5 = &pstr4;
pstr4 = &pstr3;
pstr3 = &pstr2;
pstr2 = &pstr1;
pstr1 = "Follow the white rabbit!";
printf("%s", pstr1);
}
int main() {
char ******pstr6 = NULL;
mx_deref_pointer(pstr6);
}
It does output Follow the white rabbit, but I don't think it is correct as commenting out most of the function still produces the same result. Also, I don't know how to pass anything other than NULL into the mx_deref_pointer(). Some of the guys who study with me have come up with a different mx_deref_pointer:
void mx_deref_pointer(char ******str) {
str [0] [0] [0] [0] [0] [0] = "Follow the white rabbit!";
}
It seems to work, however none of them was able to explain to me how it works. I would be extremely grateful if someone could provide a proper piece of code for this and, more importantly, explain what and how it does!
Thank you.