0

I'm currently learning c (just starting), and I'm trying to create a program that finds all the integers with 10 digits, with the conditions:

  • the first digit can be divided by 1;
  • the number represented by the first two digits can be divided by 2;
  • etc.

For example, the number 1295073846 doesn't make the cut, because 1295 is not divisible by 4 (but 1 is divisible by 1, 12 by 2, and 129 by 3).

I understand how this can be done, but I'm still struggling to manipulate arrays and pointers. Here is my code (only numToArr and the beginning of findn() should be relevant):

int numToArr(long num) { // splits a given number into an array of 10 digits
int arr[10];
int i = 9;
do {
    arr[i] = num % 10;
    num /= 10;
    i--;
} while (num != 0);
printf("%d%d%d\n", arr[0], arr[1], arr[2]);
return *arr; 
}

int join(char s[3], int n1, int n2) { // so I can get the combination of digits
    snprintf(s, 3, "%d%d", n1, n2);
    int n12 = atoi(s);
    return n12;
}

int findn() { // the output will be the list of numbers, but I'm testing with 1292500000
    long nmin = 1000000000;
    long nmax = 9999999999;
    int *arr;
    for(int i = nmin; i <= nmax; i++) {
        *arr = numToArr(1292500000l);

    // I know I can optimize this part, will work on it later
    if(arr[0] % 1 != 0) // if the first digit is not divisible by one, skip to the next number
        continue;
    else { // if it is, check for the combination of the first two digits
        char s12[3];
        int n12 = join(s12, arr[0], arr[1]);
        printf("%d\n", n12);
        break;
...

When I do

*arr = numToArr(1292500000l);

the array doesn't have the correct digits (arr[0] should be 1, arr[1] should be 2 (but it's zero)). I've tried messing with pointers and everything I could find online to solve this issue, but nothing works. Any help would be appreciated!

Thank you.

sugo
  • 25
  • 1
  • 6
  • Hi! your function `numToArr` returns an `int`, that's a "simple" value. Make it `int*` so it can return an array – David Solé González Nov 14 '20 at 16:16
  • There are numerous errors, but its fine. One for all, whenever you do `*arr` you are **dereferencing** an uninitialized pointer. For example, inside `findn` function you declare an integer pointer named `arr`, never initializing it to anything: then, in the for loop, you _dereference_ it and assign to it an int, which is the return type of numToArr. Since you never made `arr` point to anything, dereferencing it like that is undefined behavior ([here](https://stackoverflow.com/questions/4285895/where-exactly-does-c-standard-say-dereferencing-an-uninitialized-pointer-is-un) people discuss that). – Nastor Nov 14 '20 at 16:48
  • @DavidSoléGonzález thank you! – sugo Nov 14 '20 at 17:10
  • @Nastor I see, thank you! Applying the suggestions, I don't get any errors, but the variable still doesn't get the right value after applying `numToArr` in `findn`. When I print, I get 10, when I should get 12. – sugo Nov 14 '20 at 17:17

0 Answers0