Array initialization

Thread Starter

Cerkit

Joined Jan 4, 2009
287
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??
 

Mark44

Joined Nov 26, 2007
628
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??
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.
 

Mark44

Joined Nov 26, 2007
628
What is wrong with it?

Did you find this:

http://www.cplusplus.com/doc/tutorial/
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
 

mik3

Joined Feb 4, 2008
4,843
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
That's a link I know. If someone wants more he can search in google for more.
 
Top