I am facing an issue while creating a Linux module which I have simulated here.
#include <stdio.h>
#include <stdlib.h>
void changeBuff(unsigned char ** b)
{
*b[0] = 0x12;
*b[1] = 0x34;
}
int main(void) {
unsigned char * buf = (unsigned char *)malloc(sizeof(unsigned char) * 2);
buf[0] = 0x55;
buf[1] = 0xAA;
printf("%x\t%x\n", buf[0], buf[1]);
changeBuff(&buf);
printf("%x\t%x\n", buf[0], buf[1]);
return 0;
}
Long story short, I am trying to change values of an array inside a function. While the value at the first index gets changed, the same does not happen for the second index. The output of the code above is
55 aa
12 aa
Somewhat similar problems are addressed in other stackoverflow threads (e.g. Changing an array with a function in C?, Pass and change array inside function in C, Changing array inside function in C) but for some reason, even using a pointer to the character array is not working in my case (as can be seen from the output of the code in the ideone link above). What am I doing wrong here?