我有一個數組,int* array
,有超過10.000 INT的值,但我想指向每個100位的特定值,則意味着以後會有int ** matrix
,其中:
matrix[i][j]
,我想i
從我的矩陣指向array[i * 100]
,y如何替換地址?這裏是我做了什麼:指向數組
u_int8_t **matrix = (u_int8_t **)malloc(width * sizeof(u_int8_t *));
int width_cr = 0;
for (int i = 0; i < width; i ++) {
if (i % 100 == 0) {
u_int8_t *position = matrix[width_cr];
position = &array[i];
width_cr ++;
}
}
的問題是,它指向數組
[你不應該投malloc的返回(http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Eregrith
無關,但你應該改變你的循環增加到'i + = 100'並刪除'if'。 – verdesmarald
@RichardChambers我很確定OP只是想構建一個索引到現有1d數組的2d數組。 – verdesmarald