For example, the following string: abc1&qwerty22.xyz0321ijk5Q_ #*9
should produce the array: [1, 22, 321, 5, 9].
Given the array declaration char next_number[MAX_DIGITS], where MAX_DIGITS is defined to be 8, the number of consecutive digits that can be placed in the array is limited to 8.
However, when testing this code with the 9-digit number '123456789', it appears that this number is placed into the 8 character array without raising any exceptions. I would have expected that such a test to break the program, but it didn't. Further, when printing out the array of numbers, the number '123456789' is printed correctly; in other words, it was successfully stored somehow before being transferred to the array of numbers.
Can someone please explain why a 9-digit number can be placed into the 8 character array?
Thank you.
Expand|Select|Wrap|Line Numbers
- void create_array()
- {
- /* Extracted numbers cannot exceed 8 digits */
- #define MAX_DIGITS 8
- char next_number[MAX_DIGITS]; /* temp buffer used to extract numbers */
- int index = 0; /* stores the current position of 'next_number' */
- int iter; /* traverses the string */
- num_count = 0; /* stores the current position of 'series' */
- /* Traverse string extracting numbers */
- for (iter = 0; iter <= strlen(string); iter++)
- {
- /* character is a digit */
- if (string[iter] >= '0' && string[iter] <= '9')
- {
- /* Add digit to temp buffer */
- next_number[index] = string[iter];
- /* Update our position in the temp buffer */
- index++;
- }
- /* charcter is a number delimiter (a non-digit immediately following a digit) */
- else if (index > 0)
- {
- /* End the string */
- next_number[index] = '\0';
- /* Place number into array (converting to integer) */
- series[num_count] = atoi(next_number);
- /* Update position in number array ('series') */
- num_count++;
- /* Reset temp buffer */
- index = 0;
- }
- }
- return;
- }
'string' and 'series' are global variables. I know they should be parsed as parameters (using pointers/references), but it has been a while since I have touched on these topics... I am slowly relearning the C language :-)