0

I know its very naive question, but i am not able to understand what the following code does.

#include <malloc.h>
#define MAXROW 3
#define MAXCOL 4
int main(){
int (*p)[MAXCOL];
p = (int (*)[MAXCOL])malloc(MAXROW*sizeof(*p));
}

Please provide a complete explanation including the type and size of p.

It is just for learning purpose. I am not using this code in any real application.
Nitin Garg
  • 2,069
  • 6
  • 25
  • 50

5 Answers5

5

As far as I can tell, it's gibberish. You probably meant (int(*)[MAXCOL]).

In C it means that the programmer who wrote it doesn't know how void pointer typecasts work.

In C++ it means that you are allocating an array of arrays. p is an array pointer, so *p is an array of size MAXCOL, and you allocate MAXROW such arrays. The result is a "mangled" 2D array. The avantage of using this rather obscure syntax is that you get a 2D array which has every cell in adjacent memory, something you wouldn't achieve with the more commonly seen pointer-to-pointer dynamic 2D array.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • How are pointer-to-pointer dynamic 2D arrays "more commonly seen"? They are an order of magnitude more complex, and you need loops to populate them. – Lightness Races in Orbit Jul 09 '11 at 18:45
  • It appears to be the most common way to teach dynamic 2D arrays, for some reason, I don't know why, as pointer-to-pointer arrays aren't compatible with static 2D arrays, making it a pain to write generic functions handling either kind. – Lundin Jul 09 '11 at 19:51
  • Well how very odd! They are _not_ dynamic 2D arrays. They are dynamic arrays of pointers (as you said). – Lightness Races in Orbit Jul 09 '11 at 21:28
4

Supposing you meant the uncommented line (the other is the original, which is not valid C)

// p = (*)[MAXCOL]malloc(MAXROW*sizeof(*p));
p = (int(*)[MAXCOL])malloc(MAXROW*sizeof(*p));

my answer is:

In C do not cast the return value of malloc. It is at best redundant and may hide an error when present. Simply do

p = malloc(MAXROW * sizeof *p);
pmg
  • 106,608
  • 13
  • 126
  • 198
2

It's not valid code in C or C++.

So, it doesn't "do" anything at all.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

As you can learn from this question, int (*p)[MAXCOL] is a pointer to an array of MAXCOL integers.

The line p = (int (*)[MAXCOL])malloc(MAXROW*sizeof(*p)); allocates the memory for an array of MAXROW arrays of MAXCOL integers (i.e. two dimensional array), and sets p to point to it.

Community
  • 1
  • 1
Igor
  • 26,650
  • 27
  • 89
  • 114
1

I have not compiled the following code, but i think it's valid c++-code:

    typedef int[MAXROW][MAXCOL] table;

    table *p = new table;

This code is only posible if the dimensions of the array are known at compile-time.

This is the way most c++-prgrammers would define p:

    using namespace std;
    vector<vector<int> > p;

This allows for a more flexible way of programming.

G, folks, it's been a long time since i have programmed K&R C!

Bert-Jan
  • 21
  • 2