Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

segmentation fault - SegFault when passing two pointer parameter in a function in C

I'm using GCC Compiler 9.3.0 on GNU/Linux Environmental, using C17.

I have to remove the code since I can't show that to everyone, please understand.

If I remove largest_lowest call from main function there is not seg fault if I add that line there is a seg fault (core dumped)

SetData is a simple function just to set data only. Nothing more and works perfectly.

Error(When compiling)-> expected ‘point **’ but argument is of type ‘point (*)[7]’

And when I run the program there is a seg fault (core dumped).

question from:https://stackoverflow.com/questions/65882351/segfault-when-passing-two-pointer-parameter-in-a-function-in-c

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

1 Answer

0 votes
by (71.8m points)

You cannot pass a multi-dimensional array to a function taking pointer-to-pointer. Particularly since in this case, you are intend to pass a single dimension array. Fixes:

int largest_lowest(point *a[], int len) - > point a[], and
largest_lowest(&a, 7); -> largest_lowest(a, 7);

Also, please note that the function-like macro INIT_POINT_ARRAY is horrible practice and you must get rid of it. There is absolutely no reason why you should have a macro there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...