1

Im writing a program in c and i created multiple "if" to make it detect different options but every time i reference a 2d dynamic array which is in a different if section than the one who initialize it, than its marked as un-initialized.
Heres the code, the parts im talking about are in main function at the bottom in the if sections of "funprint","setfun" and "shiftfun".
The thing is that the funprint function works (i shoved it right under the "zerosfun" which makes an array for testing) but anywhere else its un-initialized and i have no idea how to solve it. i know its a total mess but im mechanical engineering student and im really bad in it...
Thanks in advance!

I tried creating different arrays and copying between them but its still un-initialized , also using pointers which didn't work as well.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int checkname(char* str) {
    int indexconvnum = 0;
    if(str[0] == '\0') {
        return 1;
    }
    while(str[indexconvnum] != '\0') {
        if((indexconvnum != 11) &&
               (str[indexconvnum] <= 90 && str[indexconvnum] >= 65) ||
           (str[indexconvnum] <= 122 && str[indexconvnum] >= 97)) {
            indexconvnum++;
        } else {
            // print error 1 sent
            return 1;
        }
    }
    return 0;
}

int strconv(char* tempconvstr) {
    int passnum = 0;
    int indexconvnum = 0;
    printf_s("%s    tempconvstr\n ", tempconvstr);
    if(tempconvstr[0] == '\0') {
        return -69;
    }
    while(tempconvstr[indexconvnum] != '\0') {
        if(tempconvstr[indexconvnum] < '0' || tempconvstr[indexconvnum] > '9') {
            return -69;
        }
        passnum = passnum * 10 + (tempconvstr[indexconvnum] - 48);
        indexconvnum++;
    }
    printf_s("%d passnum\n", passnum);
    return passnum;
}

int** zeros(int row, int col) {
    int i = 0, j = 0;
    int** tempnamearr = (int**)malloc((row) * sizeof(int*));
    printf_s("%d %d zero array", row, col);
    for(i = 0; i < row; i++) {
        tempnamearr[i] = (int*)malloc(col * sizeof(int*));
    }
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            tempnamearr[i][j] = 0;
        }
    }
    printf("mark");
    return tempnamearr;
}

void set(int** arr, int row, int col) {
    int i, j;
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            scanf_s("%d", &arr[i][j]);
            printf_s("\n");
        }
    }
}

void funprint(int** arr, int row, int col) {
    printf_s("%d %d", row, col);
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

void isSquare(char* arr, int row, int col) {
    if(row == col) {
        printf_s("%s is square lol its %dx%d", arr, row, row);
    } else {
        printf_s("%s not square lol its %dx%d", arr, row, col);
    }
}

void shift(int** arr, int shiftnum, int row, int col) {
    int i, j;
    int** tempshiftarr = (int**)malloc(row * sizeof(int*));
    for(i = 0; i < row; i++) {
        tempshiftarr[i] = (int*)malloc(col * sizeof(int));
    }
    shiftnum = shiftnum % col;
    if(shiftnum > 0) {
        for(j = 0; j < col; j++) {
            for(i = 0; i < row; i++) {
                if(i + shiftnum < row) {
                    tempshiftarr[i + shiftnum][j] = arr[i][j];
                } else {
                    tempshiftarr[i + shiftnum - row][j] = arr[i][j];
                }
            }
        }
    } else {
        for(j = 0; j < col; j++) {
            for(i = 0; i < row; i++) {
                if(shiftnum + i >= 0) {
                    tempshiftarr[i + shiftnum][j] = arr[i][j];
                } else {
                    tempshiftarr[i + shiftnum + row][j] = arr[i][j];
                }
            }
        }
    }
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            printf("%d ", tempshiftarr[i][j]);
        }
        printf("\n");
    }
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            arr[i][j] = tempshiftarr[i][j];
        }
    }
    for(i = 0; i < row; i++) {
        free(tempshiftarr[i]);
    }
    free(tempshiftarr);
}

int main() {
    // erromsg: 0=good, 1=invalid name, 2=name in use,3=only 2
    // variables,4=unkown var,5=illegal cmd,6=inv dimension.
    int f = 1, row, i, col, /*shiftnum, shifttemp,*/ passcheck = 0,
        storedfun = 0, tempshit, row1, row2, col1, col2,
        /* issquaretemp,*/ strnum;
    int **arr1, **arr2, **temparr;
    char userin[81], fun[81], funname[81],
        temprow[81] = "none", tempcol[81] = "none", array1name[12] = "\0",
        array2name[12] = "\0";
    char exitfun[] = "exit", zerosfun[] = "zeros", setfun[] = "set",
         shiftfun[] = "shift", printfun[] = "print", isSquarefun[] = "isSquare";
    do {
        printf_s("$ ");
        fgets(userin, sizeof(userin), stdin);
        userin[strcspn(userin, "\n")] = '\0';
        strnum = sscanf_s(userin, "%s %s %s %s", fun, (unsigned)sizeof(fun),
                          funname, (unsigned)sizeof(funname), temprow,
                          (unsigned)sizeof(temprow), tempcol,
                          (unsigned)sizeof(tempcol));
        if(strcmp(fun, exitfun) == 0 && strnum == 1) {
            return 0;
            // add free all mallocs in use
        } else {
            if(strcmp(fun, zerosfun) == 0 && strnum == 4) {
                if(storedfun == 2) {
                    // print errormsg 3
                    printf_s(
                        "Error: zeros cannot save more than 2 variables!\n");
                } else {
                    printf("\n%s temprow\n %s tempcol\n   ", temprow, tempcol);
                    printf("%d converted row\n", row = strconv(temprow));
                    printf("%d converted col\n", col = strconv(tempcol));
                    if(col < 0 || row < 0) {
                        // error 6
                        printf_s("Error: invalid dimension!\n");
                    } else {
                        tempshit = checkname(funname);
                        if(tempshit == 1) {
                            // error 1
                            printf_s("Error: '%s' \- invalid variable name!\n",
                                     funname);
                        } else {
                            switch(storedfun) {
                            case 0: {
                                int** arr1 = (int**)malloc(row * sizeof(int*));
                                for(int i = 0; i < row; i++) {
                                    arr1[i] = (int*)malloc(col * sizeof(int));
                                }
                                arr1 = zeros(row, col);
                                strcpy_s(array1name, funname);
                                row1 = row;
                                col1 = col;
                                storedfun++;
                                /*for (int i = 0; i < row1; i++) {
                                    for (int j = 0; j < col1; j++) {
                                        printf("%d ", arr1[i][j]);
                                    }
                                    printf("\n");
                                }*/
                                funprint(arr1, row1,
                                         col1); // un-initialized
                                break;
                            }
                            case 1: {
                                int** arr2 = (int**)malloc(row * sizeof(int*));
                                for(int i = 0; i < row; i++) {
                                    arr2[i] = (int*)malloc(col * sizeof(int));
                                }
                                arr2 = zeros(row, col);
                                strcpy_s(array2name, funname);
                                row2 = row;
                                col2 = col;
                                storedfun++;
                                break;
                            }
                            default: {
                                return 3333;
                                break;
                            }
                            }
                        }
                    }
                }
            } else {
                if(strcmp(fun, shiftfun) == 0) { // check to see missing int
                    if(strcmp(array1name, funname) == 0) {
                        row = strconv(temprow);
                        int** temparr = (int**)malloc(row1 * sizeof(int*));
                        for(int i = 0; i < row; i++) {
                            temparr[i] = (int*)malloc(col1 * sizeof(int));
                        }
                        for(int i = 0; i < row1; i++) {
                            for(int j = 0; j < col1; j++) {
                                temparr[i][j] = arr1[i][j]; // un-initialized
                            }
                        }
                        shift(arr1, row, row1, col1);
                    } else {
                        if(strcmp(array2name, funname) == 0) {
                            row = strconv(temprow);
                            int** arr2 = (int**)malloc(row * sizeof(int*));
                            for(int i = 0; i < row; i++) {
                                arr2[i] = (int*)malloc(col * sizeof(int));
                            }
                            shift(arr2, row, row2, col2); // un-initialized
                        }
                    }
                }
                // else
                //{
                // if (strcmp(fun, printfun) == 0)
                //{//check to see missing int
                //  if (strcmp(array1name, funname) == 0)
                //  {
                //
                //      funprint(arr1, row1, col1); //un-initialized
                //      /*int temparr = *arr1;
                //      funprint(temparr, row1, col1);*/
                //  }
                /*else
                {
                    if (strcmp(array2name, funname) == 0)
                    {
                        int** arr2 = (int**)malloc(row * sizeof(int*));
                        for (int i = 0; i < row; i++) {
                            arr2[i] = (int*)malloc(col * sizeof(int));
                        }
                        funprint(arr2, row2, col2);
                    }
                    else
                    {*/
                //  //error4
                //  printf_s("Error: '%s' '\- unknown variable\n", funname);
                //}
                /*}*/
                //}
                else {
                    if(strcmp(fun, setfun) == 0) {
                        if(strcmp(array1name, funname) == 0) {
                            int** arr1 = (int**)malloc(row * sizeof(int*));
                            for(int i = 0; i < row; i++) {
                                arr1[i] = (int*)malloc(col * sizeof(int));
                            }
                            set(arr1, row1, col1); // un-initialized
                        } else {
                            if(strcmp(array2name, funname) == 0) {
                                int** arr2 = (int**)malloc(row * sizeof(int*));
                                for(int i = 0; i < row; i++) {
                                    arr2[i] = (int*)malloc(col * sizeof(int));
                                }
                                set(arr2, row2, col2); // un-initialized
                            } else {
                                // 4
                                printf_s("Error: '%s' '\- unknown variable\n",
                                         funname);
                            }
                        }
                    } else {
                        if(strcmp(fun, isSquarefun) == 0) {
                            if(strcmp(array1name, funname) == 0) {
                                isSquare(array1name, row1, col1);
                            } else {
                                if(strcmp(array2name, funname) == 0) {
                                    isSquare(array2name, row2, col2);
                                } else {
                                    // 4
                                    printf_s(
                                        "Error: '%s' '\- unknown variable\n",
                                        funname);
                                }
                            }
                        } else {
                            printf_s("Error: illegal command!\n");
                        }
                    }
                }
            }
        }
    } while(f == 1);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Code Bom
  • 5
  • 1
  • 1
    In `printf_s("Error: '%s' '\- unknown variable\n"` you have `\-` which is an invalid escape sequence. What would you want that to result in? A regular `-`? If so, remove the backslash. – Ted Lyngmo May 23 '23 at 16:52
  • 2
    Kindly substitute those magic numbers with something meaningful. And don't cast `malloc()` and family. – Harith May 23 '23 at 16:55
  • 2
    `(str[indexconvnum] <= 90 && str[indexconvnum] >= 65)`??? Ouch. My eyes! Read this: [**7.4.1 Character classification functions**](https://port70.net/~nsz/c/c11/n1570.html#7.4.1) – Andrew Henle May 23 '23 at 16:59
  • The `malloc()` call inside the `for` loop in function `zeros()` has the wrong size; it should be `malloc(col * sizeof(int))`. – Ian Abbott May 23 '23 at 17:01
  • And this too: [**How to convert a string to integer in C?**](https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c) – Andrew Henle May 23 '23 at 17:01
  • 2
    [quite a few warnings](https://godbolt.org/z/da3e5erz1) that should be addressed before worrying about anything else – yano May 23 '23 at 17:02
  • In the `if` block you initialize `temparr`, but then you pass `arr1` to `shift()`. You never initialized `arr1`. – Barmar May 23 '23 at 17:04
  • What is this program actually supposed to *do*? It would probably be easier and quicker to just showcase an example outline (with perhaps a specific example of handling dynamic memory) for building this program correctly (a REPL), than it would be to correct every problem with the current example. This borders on what is appropriate for Stack Overflow's format, however. A more focused [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) would help to greatly limit the scope of this question, and would yield a more specific answer. – Oka May 23 '23 at 17:33
  • yea my code is a mess, sorry for writing it all so poorly. specifically the problem im talking about is in a function outside of main claiming that my **int array isnt initilaized because the part that initilaize it in a different if section and i cant acutally move it outside of it. – Code Bom May 23 '23 at 19:16
  • Well at a glance, you are *shadowing* the variables defined at the top of `main` with those defined within the [blocks](https://en.cppreference.com/w/c/language/scope) found within the `if` and `case` statements later. For example, `case 0: { int** arr1 = (int**)malloc(row * sizeof(int*)); ...`. If you want to assign the pointer returned from `malloc` to the variable declared at the top of `main`, then remove the `int **` which currently makes it a local definition instead: `case 0: { arr1 = (int**)malloc(row * sizeof(int*)); ...`. Whether or not this is *exactly* what you want is hard to say. – Oka May 23 '23 at 19:25
  • (cont.) And it certainly doesn't fix all the other problems with the program. Note that you open yourself up to some serious memory leaks when you have multiple locations that can assign a new pointer value to variables such as `arr1`, as a previous value will be lost if not accounted for. Again, a focused [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) with just enough code to exhibit the problem would go a long way to getting a concrete answer. – Oka May 23 '23 at 19:28
  • oka, your suggestion did help to remove the warning of un-initilaized variables....but now i get " error C4703: potentially uninitialized local pointer variable 'arr1(and arr2)' used" on the shiftfun (line 282). and i understand that my program has a lot of memory leaks and how showing the problem specifically would help....but ive been working on this for about 15 hours and i need to send it in a couple of hours so at this point i will just make something that works and think about fixing those stuffs later – Code Bom May 23 '23 at 19:36
  • Yes, just like how you have multiple locations that can assign a new pointer value to `arr1` without checking if it already holds a value, you have multiple locations that use `arr1` without checking if it *has* been given a valid value. You'll need to take more care in preventing the user from performing actions in a poorly defined sequence (e.g., printing before creating). You can start by giving setting an initializing pointers to `NULL`, which gives more information about the current *state* of the program (e.g., `if (!arr1) { /* array not created, don't try to print it */ }`). – Oka May 23 '23 at 19:51
  • initializing it with null made it all work! thank you so much! from nothing working i jumped right into minor bug fixes and adding safeguards for user input, thank you so much! – Code Bom May 23 '23 at 20:14

0 Answers0