Issues with your account? Bug us in the Discord!

(C/C++) two-dimensional dynamic array

RubberEagleRubberEagle What's a rubber eagle used for, anyway?
I'm currently working on a little program, and i need to make a 2 dimensional char-array, where (at least) one dimension isn't hardcoded.
(i know how to make a one-dimensional dynamic array using malloc and calloc)

it is a file list, that is going to be submitted to another function, and the dynamic part is the number of files. I'd use a stupid simple trick like a 1d array of structs (that include a char array), but the other function only understands a 2d char array (no, reprogramming the second function is not an option (for now) [img]http://216.15.145.59/mainforums/wink.gif[/img] )

Can anyone help? Please?
Thanks
RubberEagle

Comments

  • PJHPJH The Lovely Thing
    Ack, it's been so long I coded C++ last time. I can't even remember what malloc and calloc functions did.

    I'm not sure, but if I recall correctly the dynamic arrays were done using pointers pointing to the last block of the array in the memory, so you didn't have to allocate any certain amount of memory for the arrays before hand. Or was it that the pointer pointed to a place in memory where then was the information where the last block of the array is located? Like I said, it's been a long time. [img]http://216.15.145.59/mainforums/rolleyes.gif[/img] If you don't know how to use pointers check out [url="http://www.cplusplus.com/"]http://www.cplusplus.com/[/url] or some other C-language website.

    Biggles IIRC knows C++ pretty well. You there Les Bigg? [img]http://216.15.145.59/mainforums/biggrin.gif[/img]

    - PJH
  • PJHPJH The Lovely Thing
    Here you should find what you need: [url="http://www.cplusplus.com/cgi-bin/search.cgi?q=dynamic+arrays"]http://www.cplusplus.com/cgi-bin/search.cgi?q=dynamic+arrays[/url]

    - PJH
  • PJHPJH The Lovely Thing
    Read this: [url="http://www.cplusplus.com/doc/tutorial/tut3-3.html"]http://www.cplusplus.com/doc/tutorial/tut3-3.html[/url]

    - PJH
  • RubberEagleRubberEagle What's a rubber eagle used for, anyway?
    dynamic arrays with malloc and calloc work that way, that you create a (NULL)pointer, and then, when you know how much memmory you need, you allocate it with malloc or calloc...
    thanks for the link, let's see what it does.. [img]http://216.15.145.59/mainforums/smile.gif[/img]
  • RubberEagleRubberEagle What's a rubber eagle used for, anyway?
    Thanks a lot, the page you linked to didn't solve it, but the next page did it (i hope, have to try it).. i totally forgot about "new" and it looks like it accepts variables as array dimensions [img]http://216.15.145.59/mainforums/smile.gif[/img]
  • PJHPJH The Lovely Thing
    Good luck with it. I'm glad if it helped. [img]http://216.15.145.59/mainforums/smile.gif[/img]

    - PJH
  • BigglesBiggles <font color=#AAFFAA>The Man Without a Face</font>
    Yep, new and delete should do the trick. Make sure when you delete it to use the correct form of delete. For arrays, this is delete[].

    If you want to go all the way, make it a class with accessor functions and stuff like that. That way, you can encapsulate all the allocation and deallocation stuff. If you want to go even further and promote code reuse, make it a templated class. [img]http://216.15.145.59/mainforums/smile.gif[/img]

    ------------------
    [url="http://www.minbari.co.uk/log12.2263/"]Never eat anything bigger than your own head.[/url]
    "Nonono...Is not [i]Great[/i] Machine. Is...[i]Not[/i]-so-Great Machine. It make good snow cone though." - Zathras
  • RubberEagleRubberEagle What's a rubber eagle used for, anyway?
    yep, but making it a class would again require me to reprogram the second function... (it's a file-archive creator)
Sign In or Register to comment.