0

[Solved]

So i just started using pointers in c++ and i came up with a problem ... So what i need to do is take a number from a 2D array and sum it with a number below example we have a 2D array : 4 3 1 2 3 3 4 5 5 6 7 8 9 6 Then the answers would be : 1 + 3, 2 + 4, 3 + 5, 3 + 5, 4 + 6,5 + 7, 5 + 8 , 6 + 9, 7 + 6 So i wrote a program that kinda works... It shows the correct answer however there's an segmentation error.... Program: https://code.sololearn.com/caHS98LDz6cz/?ref=app

18th Feb 2020, 8:57 AM
Alice
Alice - avatar
1 Antwort
+ 2
You allocated for an array a space of *n elements. Its last index is *n - 1. But in line 49 you access to element with index *n: (a + (i + 1)) i is iterates from 0 to *n - 1, and on the last step of loop i becomes *n - 1, so (a + (i + 1)) = a + *n, which contains address of garbage value. Change condition of for-loop in line 43 to *n-1, you should not iterate over last row of the array.
18th Feb 2020, 10:48 AM
andriy kan
andriy kan - avatar