0

Possible Duplicate:
dynamic memory for 2D char array

hi i am wondering how can i declare a dynamic 2D array where the size of array is set by the number of inputs enter. Example:

user input:
1 2 3
4 5 6
7 8 9

array[0][0] == 1    array[0][1] == 2    array[0][2] == 3
array[1][0] == 4    array[1][1] == 5    array[1][2] == 6
array[2][0] == 7    array[2][1] == 8    array[2][2] == 9
Community
  • 1
  • 1
menotyou
  • 149
  • 1
  • 1
  • 7

1 Answers1

0

Use the malloc() function. Hint: you'll need pointers to pointers if you want to emulate a two-dimensional array.

  • I think the problems is that we need to scan the input to know the size of the array to be create... but then where to save the input temporaly?? We make an arbitrary length array and by need realloc it? – qPCR4vir Feb 01 '13 at 14:35
  • 1
    @qPCR4vir For example, yes. Or ask the user to enter the number of rows and columns. –  Feb 01 '13 at 14:38
  • 1
    i am trying to avoid having the user input the number of rows and columns. – menotyou Feb 01 '13 at 14:40
  • 1
    @menotyou Then keep track of whenever you get valid input? Maybe utilize the return of `scanf`? Then use `realloc` to change the amount of memory you have allocated. – Keith Miller Feb 01 '13 at 14:41
  • 1
    @menotyou Then use realloc, as qPCR4vir mentioned. –  Feb 01 '13 at 14:41
  • I can write a procedure for this, but I think it is a very common situation and I wanted to see a correct one, long tested by one of you, experienced C programers. I had the problem too! – qPCR4vir Feb 01 '13 at 14:45
  • pointers to pointers are not really necessary for this. a single 1d array is enough if you are able to pack the rows into one. – Andreas Grapentin Feb 01 '13 at 14:54
  • 1
    @AndreasGrapentin Of course it's not mandatory, but I assumed OP wanted the convenient `array[row][column]` syntax. –  Feb 01 '13 at 14:55
  • Try looking up `calloc()`and see if it suits better – noMAD Feb 01 '13 at 14:59
  • @H2CO3 hm, that makes sense. I'm wondering if malloc'ing a 2d array could, depending on the malloc implementation, actually be faster than having to calculate the offset in the 1d array on each access. – Andreas Grapentin Feb 01 '13 at 15:01
  • Downvoter: care to comment? –  Feb 01 '13 at 18:45
  • It is not true that pointers to pointers are needed to emulate a two-dimensional array, and it is typically inefficient to do so. C supports variable-length arrays. Once the row length is known, the pointer may be declared with `TYPE (*p)[length]`, assigned space with malloc, and used as `p[i][j]`. – Eric Postpischil Feb 01 '13 at 18:47
  • @EricPostpischil Did you read my comment above? Will you revoke your (**unjust**) downvote if I change "you'll need" to "you most probably intend to"? –  Feb 01 '13 at 18:51
  • Your comment is wrong: Assuming the OP wants `array[row][column]` syntax does not mean pointers to pointers are needed. The declaration I showed shows how to get the syntax without pointers to pointers. – Eric Postpischil Feb 01 '13 at 18:53
  • @EricPostpischil Tricky: you don't answer my question, instead you edit your previous comment so now it seems I'm acting dumb. Clever, indeed. –  Feb 01 '13 at 18:55
  • I did not edit the comment. – Eric Postpischil Feb 01 '13 at 18:57