Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are using pointers to arrays, so you shouldn't index them directly, as <code>p[i]</code> will give <code>*(p+i)</code> i.e. an array following the one pointed to by p, rather than an element of p.</p> <p>In C, <code>void*</code> will convert to any pointer type, so you don't need to cast the result of malloc. If you do put the casts in, it can mask errors, for example if you are trying to assign to a non-pointer ( such as <code>p[i]</code> ).</p> <p>In the malloc for p, <code>sizeof(int (*p)[a])</code> should either use a type or an expression, not a declaration. <code>p</code> is a pointer to an array of pointers to arrays of int, so the type of the elements of <code>*p</code> is <code>int (*)[]</code>.</p> <p>So this compiles without error or warning on gcc:</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int main ( void ) { int i, j, n, a, b; int ( * ( * p ) [] ) []; printf ( "\n\tEnter the size of the matrix in the form aXb\t\n" ); scanf ( "%dX%d", &amp;a, &amp;b ); p = malloc ( b * sizeof ( int ( * ) [] ) ); for ( i = 0;i &lt; b;i++ ) { ( *p ) [i] = malloc ( a * sizeof ( int ) ); printf ( "\t\bEnter Column %d\t\n", i ); for ( j = 0;j &lt; a;j++ ) scanf ( "%d", &amp; ( * ( *p ) [i] ) [j] ); } return 0; } </code></pre> <p>However, since there is no advantage in using a pointer to an array against using a pointer to its first element but it does mean you have to dereference before taking the element, it is much easier to use the pointer to a pointer form.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload