Why is runtime error with my C code? input 9 output [4,2]

  • 0

    When I run the code in my computer, it turns out all right.
    However, OJ says wrong.

    Input: [9] Output: [4,2] Expected: [1,0]

    Here is my code:

    int* plusOne(int* digits, int digitsSize, int* returnSize) {
    for (int i = digitsSize-1; i > -1; --i) {
    	if (digits[i] == 9) {
    		digits[i] = 0;
    	} else {
    		*returnSize = digitsSize;
    		return digits;
    *returnSize = digitsSize+1;
    int newArr[*returnSize];
    memset(newArr, 0, *returnSize);
    newArr[0] = 1;
    return newArr;}

  • 0

    You have returned a local array whose storage would be recycled at the end of function 'plusOne'. The caller would get unknown value.

    You compiler should warn this return value.

  • 0

    got it, thanks!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.