Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This statement has several problems:</p> <pre><code>p=(int (*(*p)[b])[a])malloc(b*sizeof(int (*p)[a])); </code></pre> <p>First, <code>malloc</code> returns a <code>void*</code>. You are casting that pointer using <code>(int (*(*p)[b])[a])</code> which yields a value, not a data type. That isn't a valid cast, so that's one reason that the compiler is yelling at you. At this point, <code>p</code> hasn't been initialized so the de-referencing taking place here can crash your program if this statement was executed.</p> <p>Inside your <code>malloc</code> call, you are using <code>sizeof(int (*p)[a])</code>. The statement <code>int (*p)[a]</code> isn't a valid C statement.</p> <p>It seems that you are making this a bit more complex that it needs to be. There are two ways of building a 2D array. You can build an array using <code>malloc(a * b * sizeof(int))</code> as Reinderien explains. You can also build a 1D array of pointers, each pointing to an array of type <code>int</code>. From your code, it seems you are trying to do the latter.</p> <p>The easier way to do this would be something like this:</p> <pre><code>int **p; ... get input from user ... // Declare an array of int pointers of length b p = malloc(b * sizeof(int*)); // For each int* in 'p' ... for (i = 0; i &lt; b; ++i) { // ... allocate an int array of length 'a' and store a pointer in 'p[i]' .. p[i] = malloc(a * sizeof(int)); // ... and fill in that array using data from the user printf("\t\bEnter Column %d\t\n"); for(j = 0; j &lt; a; j++) scanf("%d", &amp;p[i][j]); } </code></pre> <p>Using this method of building a 2D array allows you to use the syntax <code>p[x][y]</code>. Since <code>p</code> is a pointer-to-pointer, <code>p[x]</code> is a pointer to an array and <code>p[x][y]</code> is an item in the pointed-to array.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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