+ 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
7 odpowiedzi
+ 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()
+ 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.
+ 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
+ 1
Coder Kitten Excellent code 😀, you must have your name on it, you deserve it.
Thanks a lot for your time...
+ 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
+ 1
Coder Kitten Would you mind explaining what high water mark is???
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;