Search code examples
c++carraysfunction-calls

c/c++ | int * excepted, int given (returning a pointer??)


So I am making a program to find the max possible substraction. If n is, f.e 9284, the minimum number which can be made with the digits of 9284 is 2489 and the maximum one is 9842. the function max_substraction returns 9842 - 2489 as output.

The problem is that whenever I run the program and type a number, the program crashes. I did try to debug and I was told that the function "to_digits" was excepting int * but int given which is really strange. Ain't I returning a pointer? ...

#include <stdio.h>

int valid(int);
int length(int);
int to_digits(int);
int to_num(int *, int);
void sort_digits(int *, int);
int max_substraction(int *, int);

int i,j;

int main()
{
    int num;
    scanf("%d", &num);
    if(!valid(num))
    {
        printf("Invalid!\n");
    }
    else {

        printf("%d", max_substraction(to_digits(num), length(num)));
    }
    return 0;
}

int length(int num){

    int cpy_of_num = num, len = 0;
    do {
        cpy_of_num /= 10;
        len++;
    } while(cpy_of_num != 0);

    return len;
}

int valid(int num){
    int len = length(num);
    if(len<2 || len>9){
        return 0;
    }
    return 1;
}
int to_digits(int num){
    int len = length(num), cpy_of_num = num;
    int digits[len];
    for(i=len-1; i>=0; i--){
        digits[i] = cpy_of_num % 10;
        cpy_of_num /= 10;
    }
    return *digits;
}
int to_num(int *digits, int len){
    int new_num = 0, mult = 1;
    for(i=len-1; i>=0; i--){
        new_num += digits[i]*mult;
        mult *= 10;
    }
    return new_num;
}
void sort_digits(int *digits, int len){
    for(i=1;i<len;i++){
        for(j=0;j<len-1;j++){
            if(digits[j] > digits[j+1]){
                int temp = digits[j];
                digits[j] = digits[j+1];
                digits[j+1] = temp;
            }
        }
    }
}
int max_substraction(int *digits, int len){
    sort_digits(digits, len);
    int max_num_digits[10], min_num_digits[10];
    for(i=0; i<len; i++){
        min_num_digits[i] = digits[i];
        max_num_digits[i] = digits[len-i-1];
    }
    int min_num = to_num(min_num_digits, len);
    int max_num = to_num(max_num_digits, len);

    return max_num - min_num;
}

Solution

  • The problem is in the function call

      max_substraction(to_digits(num), length(num))
    

    as per the signature,

    int max_substraction(int *digits, int len)
    

    the first param should be a int *, but to_digits() returns you an int!!

    Then, a return statement like

      return *digits;
    

    returns you an int value, not a pointer.

    That said, even if you tried, in it's current form, digits array cannot be returned from to_digits(), as it is local to the function. You need to have static storage if you want to return the array. Now, since by definition, VLAs cannot have static storage, you may want to use memory allocator functions, like malloc() and family.