I'm stuck in C array pointer and passing it as argument into c functions. Here is the code where issues are involved:
char *src_filename1 = NULL;
AVFrame *frame = NULL;
AVFrame *frames_video1[3];
AVFrame *frames_video2[3];
int decode_packet(int *got_frame, int cached, AVFrame *frames_video[])
{
...
frames_video[video_frame_count] = frame; //video_frame_count is 2 at the moment
...
}
int main()
{
decode_video(src_filename1, frames_video1);
...
printf("frames_video1[i]->pkt_size: %d\n", frames_video1[i]->pkt_size);
...
}
int decode_video(char *src_filename, AVFrame *frames_video[])
{
...
decode_packet(&got_frame, 0, frames_video);
...
}
I could compile the code, but when I executed it, I encountered segmentation fault caused by frames_video1[i]->pkt_size
How shall I pass arguments in this context? I find when dealing with char*[] and char** especially when using them as function arguments, my hands always get dirty...
EDIT NEW CODE: (which works!)
char *src_filename1 = NULL;
AVFrame *frame = NULL;
AVFrame frames_video1[3];
AVFrame frames_video2[3];
int decode_packet(int *got_frame, int cached, AVFrame frames_video[])
{
...
AVFrame tmp;
tmp = *frame;
frames_video[video_frame_count] = tmp;
...
}
int main()
{
decode_video(src_filename1, frames_video1);
...
printf("frames_video1[i].pkt_size: %d\n", frames_video1[i].pkt_size);
...
}
int decode_video(char *src_filename, AVFrame frames_video[])
{
...
decode_packet(&got_frame, 0, frames_video);
...
}
Let me briefly summarize the main issue of the old code: It had little to do with initialization, the main problem was the passing array of pointers as argument, was really about passing argument all the way properly: after hearing the suggestion from @SF., I changed the argument from AVFrame *frames_video[] to AVFrame frames_video[], and supprisingly everything worked well! It also means AVFrame frames_video1[]
has been finally successfully "received" by decode_packet
function... "Arrays behave as pointers in so many ways that you're getting drowned in pointer-to-pointer bog.", that's what he said. Actually, I still don't quite really understand how c pointers (especially when associated with arrays) work in a deeply sense...