Please Help me find the Bug | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Please Help me find the Bug

https://code.sololearn.com/ca17a13a24A7 ================== In the above code I was trying make :- 1. When a person adds an element in an array (add function). 1.2. The add function reallocates memory to the array w.r.t to the count it tracks. 2. The function display is to display what in the array w.r.t to the count it tracks (display function). ================== No Output is given... Anybody who can fix this, will be highly appreciated... Thank You

23rd Mar 2021, 7:53 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
7 Answers
+ 3
Because you pass the address of pointer, you should use double pointer as the data type of p in add(), and because you don't return value in add(), use void instead of int. void add(int **p, int elem) Change all the p to *p in add(). p refers to the address of the pointer. By dereferencing it you get the actual pointer itself, which is what we want to allocate to. No need to pass address of pointer in display(). Missing semicolon in printf("\n"); in display()
23rd Mar 2021, 8:16 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Line 12 should be *(*p+(count-1))=elem; the * before p needs to be added. and change the return type of the function to void since you don't return.
23rd Mar 2021, 8:24 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Coder Kitten Umm actually I had no knowledge of double pointer yet, I did not know that I would require it in this. So I had the doubt, but thanks for your suggestions... Also I really liked the way you explained, though I haven't got that far, but if possible from your side, I would really like to see how it would look with those safe handling... In other words, I would like to see a code with all those handling, if you get time, please Thanks
23rd Mar 2021, 8:36 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 1
Coder Kitten Excellent code 😀, you must have your name on it, you deserve it. Thanks a lot for your time...
23rd Mar 2021, 9:15 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 1
CarrieForle and Coder Kitten a lot of thanks for both of you, your suggestions helped me a lot, really!!! If I could, I would mark both of answers as the best, but unfortunately cannot... Thanks Again
23rd Mar 2021, 9:18 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 1
Coder Kitten Would you mind explaining what high water mark is???
23rd Mar 2021, 9:54 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
@CarrieForle Doesn't solve the issue yet, updated it, https://code.sololearn.com/ca17a13a24A7 In my system, it gives this error :- new 1.c: In function 'add': new 1.c:12:16: warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion] *(p+(count-1))=elem;
23rd Mar 2021, 8:21 AM
Arun Bhattacharya
Arun Bhattacharya - avatar