How do I create a 2-dimensional runtime array in C? I know how many "columns" it has and that is 3 but the number of rows is determined at runtime. How do I properly allocate the room for that?
char* y="hello";
char*** x;
int i,j;
x=(char***)malloc(num_cols*num_rows*sizeof(char*));
for(i=0;i<numcols;i++)
for(j=0;j<numcols;j++)
x[j]=y; John Fill
But in future you probably will want to use this code in C++.
For example we are use both code in our programm C and C++ and sometime you need to call C++ from C so you just compile it as C++. In this case (void*) will not be cast by default to (int(*)[3]) at least in gcc, you will have a compilation error. So it is never hurt to use a cast.
Of cause you can skip this cast for your homework.
Also this type int(*)[3] can help you to pass this array as an argument in some function
int func(int(*)[3] in_paiArray)
{
}
I have spent some time to find the right type for this argument.
But second advice about using paiArray to get it size is very wise
That's a good point and this is an important difference between C and C++. C++, indeed, doesn't allow such conversions to occur implicitly so the cast is required if you were using Lim's example in C++ code.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.