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);
}