I extracted the function in question from my main project and simplified it in order to demonstrate the problem I have:
#include <stdlib.h>
#include <string.h>
void read_input(unsigned char **buff, unsigned int *length){
unsigned int buff_size = 36;
*buff = (unsigned char *) malloc(buff_size);
memset(*buff, 0, buff_size);
unsigned char chr;
while((chr=getchar()) != '\n' && chr != EOF){
*buff[*length] = chr;
printf("%c\n", *buff[*length]);
++(*length);
}
}
int main(int argc, char **argv){
unsigned char *buff;
unsigned int length = 0;
read_input(&buff, &length);
printf("%s, %i\n", buff, length);
}
If I run the program, and type test
, the output is:
t
Segmentation fault (core dumped)
Why is this happening and how can I fix it?