Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

c - How to print a unsigned char array from a pointer of a structure?

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The type of func2 is - int (struct test *) and you are passing func2 pointer as 2nd argument to func1() whose 2nd parameter type is int (*)(struct test). This is type mismatch. You need to make correction in 2nd parameter type of func1() function:

void func1( struct test *test_func1, int (*f)(struct test *))
                                                         ^^^

The parameter type of func2() function is - struct test *. That means, it can receive address of variable of type struct test. Here,

    // calling function
    (*f)(*test_func1);

pointer f hold the address of func2() function and argument that you are passing is of type struct test. Again the type mismatch. Instead, it should be

    // calling function
    (*f)(test_func1);
        ^^^

because the test_func1 is of type struct test * which is same as the type of parameter of func2() function.

One more point, you don't need to give & with test_func2->in when printing its value in func2() function:

    printf("Inside func2, msg is : %s
", test_func2->in);
                                         ^^^

Same is for func1() function when printing test_func1->in.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...