Array initialization

Discussion in 'Programmer's Corner' started by Cerkit, Mar 3, 2009.

  1. Cerkit

    Thread Starter Active Member

    Jan 4, 2009
    275
    3
    I'm working on a C program. I need to create a row with four columns whereby the length of the columns is determined at run time. I think I should use pointers but not sure how exactly to do it. Can someone assist me??
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    With all due respect, mik3, the boilerplate link you provide for any and all questions about C or C++ is not very helpful.
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    It seems to me that you want a two-dimension array with an arbitrary number of rows and four columns. For such an array to change size at run time, you will need to allocate space for the original array from the heap, using malloc(). When the size changes, you will need to reallocate memory, which you can do by using realloc(). When your application doesn't need the memory any longer, use free() to return the heap memory. Look at the documentation of the standard library functions malloc, realloc, and free. All three are declared in stdlib.h.
     
  5. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    What is wrong with it?

    Did you find this:

    http://www.cplusplus.com/doc/tutorial/
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Yes, I found that tutorial, which is OK as far as it goes. C++ is vastly more complicated than C, especially in its OO aspects such as polymorphism, inheritance, data-hiding, and so on, not to mention other details like what happens when you don't provide a default constructor and/or copy constructor and/or destructor.

    My objection is that to any and all C or C++ questions, you provide that link and nothing else. IMHO, that's not much help. A better response, in my opinion, would at least provide the name of a topic that the poster could search for, to find information about what he/she is looking for.

    Mark
     
  7. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    That's a link I know. If someone wants more he can search in google for more.
     
  8. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Start a new threat if you need help.
     
Loading...