How can I declare an array with a variable, but without using malloc() in C? -
so have create 2d array going used print histogram. know array going 52 elements wide, height of array going equal max + 1
, different every time function in gets called. ideally, line accomplish following function:
char histplot[max+1][52] = "";
as writing code, however, learned c, or @ least compiler, not allow array declared without constant value. professor of class i'm writing code not clear on whether or not use malloc() assignment. assuming cannot using malloc()
, there way can create array histplot[][]
have height of max + 1
?
edit: i've accepted there no real way accomplish i'm trying without using malloc()
or similar function. did manage find solution doesn't involve functions or 2d array @ all, thank replied!
some systems provide alloca
function. (however, not standard c.)
outside of that, there's no real way dynamically sized array.
i can offer following more or less silly workarounds:
- use
calloc
orrealloc
allocate memory (following letter of rule can't usemalloc
not spirit) - ... or
mmap
(if platform has that) - if doesn't have real array, use temporary file store data (access via
fseek
,fread
,fwrite
) - if can use linked list instead, can build 1 of on stack recursive function calls
Comments
Post a Comment