修改使用malloc创建的int数组中的值

时间:2019-06-01 09:36:51

标签: c arrays

这就是我想要做的: 我有这样声明的int数组:

int *istructions = malloc(35 * sizeof(int));

然后我将此数组提供给函数

该功能需要一个int *instructions[],我使用&instructions

一旦我做了我的事情,并且试图将东西保存到数组中,instructions[0]会被毫无问题地访问,并且值将被保存,但是任何其他单元格给我的错误是“无法访问地址0x0的内存” 。 我该如何解决?

我要保存的东西只是2位数字

这是我正在使用的功能。我删除了整个扫描文件,并将其保存到char数组部分中,基本上,该功能应该读取每一行,将第一部分转换为int(这种情况一直存在,因为这就是输入文件应该是的样子) ),然后将该数字保存到int数组中

int makenumber(char number){

    int result = 0;
    int i = 0;
    int lung = 0;
    lung = strlen(number);

    for(i = 0; i < lung; i++){
        result = result * 10 + (number[i] - '0');
    }

    return result;
} /*just a little function that takes a char array and converts it into an actual number*/

void populateArray(int *instructions[]){

    char file[DIM][DIM]; /* DIM is 500, just a big number to not have storage problem,each line is like "number ; random text" what number it is and what comes after it isn't relevant because the function just read chars that are numbers and stops as soon as it doesn't read them anymore*/
    int i = 0;
    int m = 0;
    int contIsr = 0; /* these are just counters that i use to read the lines*/
    int lung = 0;
    char number[3]; /*this is gthe array where i store the number before making it an actual number. I use lung to move into different cells to save the other digits*/
    int firstnumber = 0; /*i need to ignore the first actual number i read, this helps me do that*/
    int result - 0; /*the variable where I store the converted number*/

    for(i = 0; i < 35; i++){ /*35 is the numbers of lines in the file, it's supposed to be a variable, but i just put the number here for the example*/
        while(file[i][m] >= 48 && file[i][m] <= 57){ /*I used ascii values to determine what character where numbers*/
            number[lung] = file[i][m];
            m++;
            lung++; /*found a number, save it into number[], increase counters to save other digits*/
        }

        result = makenumber(number);

        if(result > 0){ /*since some lines from the file can be blank i might get a negative value from makenumber so this is why i use this if condition*/
            firstnumber++;
        }
            if(firstnumber != 1){ /* as i said i just need to avoid the first actual number i read*/
                *istruzioni[contIstr] = result;
                contIstr++;
            }
        }
        m = 0;
        lung = 0; /* I reset m and lung to read the next line in the array from the start*/
    }
}                

正如我所说的,问题是在contIstr不同于0时立即发生。访问指令[0]时没有任何问题,其他任何单元格都会给我错误

0 个答案:

没有答案