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
707 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 Reply

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.


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

...