I'm trying to call a function via function parameter. The function I'm trying to call takes a structure as a parameter. I'm able to call the desired function but while trying to print an unsinged char
array, I'm getting Segmentation fault (core dumped)
.
My code is,
//char_array.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <inttypes.h>
# define SIZE 20
static struct test{
unsigned char in[SIZE];
unsigned char out[SIZE];
}teststruct;
int func2 (struct test *test_func2){
printf("Inside func2 function
");
printf("Inside func2, msg is : %s
", &test_func2->in);
return 1;
}
void func1( struct test *test_func1, int (*f)(struct test)){
printf("Inside func1, msg is: %s
", &test_func1->in);
// calling function
(*f)(*test_func1);
}
int main(){
struct test test_main;
memcpy(teststruct.in,"This is test ", sizeof(teststruct.in));
test_main=teststruct;
//check values
printf("test_main.in is: %s
", test_main.in);
// calling func2 from func1
func1(&test_main,func2);
}
Output:
$./char_array
test_main.in is: This is test
Inside func1, msg is: This is test
Inside func2 function
Segmentation fault (core dumped)
I'm compiling my code like following,
gcc -O0 -g -Wall -Wextra -pedantic -fomit-frame-pointer -o char_array char_array.c
Any help what I'm doing wrong here?