+ 1

How do I stop calling a function when it returns NULL pointer? [C]

I wrote a void function that takes a string as an input, then calls function find_first which returns a pointer to the first digit of a first number in a string, that satisfies the condition: first+second=third number. Function find_first will return NULL pointer if there is no such number or there are less than 3 numbers in total. Now, that void function needs to print every pair of numbers that satisfies the previous condition. Every time you call find_first from this void function, it returns the next such number. I am using one pointer to convert the first number to integer and a second pointer to convert second number. Everything works well and prints those numbers, but once there are no more numbers, my program crashes and displays "process exited with code 139". How do I terminate it? Example: char string[ ]={"numbers 1 2 3 5 78"}; find_first returns pointer to number 1 1, 2 is being printed find_first returns pointer to number 2 2, 3 is being printed At the end it crashes and I get a memory error

1st Jan 2020, 11:23 PM
ja008
ja008 - avatar
1 Antwort
0
Martin Taylor thank you! While loop didn't work for whatever reason but do-while does.
2nd Jan 2020, 3:03 AM
ja008
ja008 - avatar