Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is int(*)[3] a multidimensional array?

Status
Not open for further replies.

DonK9

Programmer
Aug 8, 2002
2
0
0
US
I thought int(*)[3] defined a multimensional array, so I made an int** and allocated the array dynamically. The compiler says, "Can't convert from int** to int(*)[3].

How should this be done?

The library that I am using demands an array of triangles and their vertices.
 
Here is an example on how to use a "2D dynamical array".

#include<stdio.h>
#include<stdlib.h>
#define _MAXSIZE_ 100
typedef int RowArray[_MAXSIZE_];
void main()
{
int ROW, COL;
int i, j, k;
RowArray *array; /*declaration of the array*/
k = 1;
/* input the array */
printf(&quot;Array input\n&quot;);
printf(&quot;Enter the value for the \&quot;ROW\&quot;: &quot;);
scanf(&quot;%d&quot;,&ROW);
printf(&quot;Enter the value for \&quot;COL\&quot;: &quot;);
scanf(&quot;%d&quot;,&COL);
/*allocate memory for the array*/
array = ( RowArray * )malloc(ROW * COL * sizeof(int));
/*input the array*/
for( i = 0; i < ROW; i++ )
{
for( j = 0; j < COL; j++ )
{
printf(&quot;value%d = &quot;, k );
scanf(&quot;%d&quot;,&array[j]);
k++;
}
}
/* output the array*/
printf(&quot;\nArray output\n&quot;);
for( i = 0; i < ROW; i++ )
{
for( j = 0; j < COL; j++ )
{
printf(&quot;array[%d][%d] = %d\n&quot;, i, j, array[j]);
}
}
}
 
Actually, I am making a call into a library that demands an int(*)[3] and I'm not absolutely sure what the int(*)[3] syntax means.

My question is how do I dynamically create an int(*)[3] ??
 
Code:
#include <malloc.h>
#include <stdio.h>

int
function(int nbTriangle, int (*triangles)[3])
{
  return 0;
}

int
main(int argc, char *argv[])
{
  int array1[10][3];
  int (*array2)[3] =
    (int (*)[3])malloc(10 *
                       sizeof(*array2));

  (void)fprintf(stdout,
    &quot;sizeof(array1)  = %d\n&quot;,
    sizeof(array1));

  (void)fprintf(stdout,
    &quot;sizeof(*array1) = %d\n&quot;,
    sizeof(*array1));

  (void)fprintf(stdout,
    &quot;sizeof(array2)  = %d\n&quot;,
    sizeof(array2));

  (void)fprintf(stdout,
    &quot;sizeof(*array2) = %d\n&quot;,
    sizeof(*array2));

  function(10, array1);
  function(10, array2);

  return 0;
}
 
int i = 255, j = 10,k = 20;

int **ip;
int *n[3] = {&i,&j,&k};

ip = n;

cout<<ip[0][0]<<&quot; &quot;<<ip[1][0]<<&quot; &quot;<<ip[2][0]<<endl;


I hope that helps. It outputs &quot;255 10 20&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top