| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
dynamically allocate both dimensions of a 2d array
I need to know how to dynamically allocate both dimensions of a 2d array such as array[row][col]
but I do know know how to correctly do this as well as how to store info into spaces. so far I have tried... main(){ char *array; int row, col, rowInput, colInput; printf("blah blah rows"\n); scanf("%d", &rowInput); printf("blah blah cols"\n); scanf("%d", &colInput); array = calloc(row*col*sizeof(char*)); for(row=0; row<rowInput; row++){ for(col=0; col<colInput; col++){ array[row][col] = '0;' } } return 0; } |
|
#2
|
|||
|
|||
|
Look at my reply here:
http://forums.devarticles.com/t11075/s.html The C function allocateMatrix() will dynamically allocate memory for a two dimensional array of ints. In this example both dimensions are the same, but the code could easily be modifed to allow a different dimension for rows and collumns. The code could also be easily modifed to create an array of floats, chars, etc. There's also a sniplet from main that shows you how to declare the pointers in main and how to call allocateMatrix(). Let me know where to send the bill. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > dynamically allocate both dimensions of a 2d array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|