c - Expression must be a modifiable lvalue when mallocing -
i understand question has been asked before, can't quite narrow down i've done wrong here
int* arr[2]; arr = (int (*)[2]) malloc(sizeof(int) * size);
why visual studio telling me expression must modifiable lvalue? i'm trying create array constant column size, since two, rows vary.
arr
array of pointers. cannot assign once initialized. since trying set value return value of call malloc
, want pointer array. in case, use:
int (*arr)[2];
also, don't cast return value of malloc
. see do cast result of malloc?.
use
int (*arr)[2]; arr = malloc(sizeof(*arr) * size); // ^^^^^^^^^^^^ speculating need, // not sizeof(int)
if want arr
point array of 15 x 2
objects, need use:
arr = malloc(sizeof(*arr) * 15);
Comments
Post a Comment