Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Arrays

RicardoFurtadoRicardoFurtado Member Posts: 205
I'm using MS Visual C++ 2008 and i'm trying to use an array containing some complex information, but the compiler states an error:
error C2440: 'Initializing': cannot convert from 'char (*)[17] to 'char *'

my array is this:


char* Arr=new char[184][17];


Arr[0][0]="1";
Arr[0][1]="Basion";
Arr[0][2]="Ba";
Arr[0][3]="Basio.jpg";
Arr[0][4]="Basio.avi";
Arr[0][5]="Ponto mais inferior do contorno anterior do foramen Magnum";
Arr[0][6]="False";
Arr[0][7]="";
Arr[0][8]="";
Arr[0][9]="";
Arr[0][10]="True";
Arr[0][11]="False";
Arr[0][12]="";
Arr[0][13]="52";
Arr[0][14]="1";
Arr[0][15]="";
Arr[0][16]="False";

Arr[1][0]="2";
Arr[1][1]="Sella";
Arr[1][2]="S";
Arr[1][3]="Sela.jpg";
Arr[1][4]="Sela.avi";
Arr[1][5]="";
Arr[1][6]="False";
Arr[1][7]="";
Arr[1][8]="";
Arr[1][9]="";
Arr[1][10]="True";
Arr[1][11]="False";
Arr[1][12]="";
Arr[1][13]="68";
Arr[1][14]="1";
Arr[1][15]="";
Arr[1][16]="False";


... continues ...
I belive i'm not declaring correctly the array.
Can you help?

My thanks in advanced

Comments

  • queshangxintouqueshangxintou Member Posts: 1
    error in this line char* Arr=new char[184][17];

    char** Arr=new char[184][17];
  • RicardoFurtadoRicardoFurtado Member Posts: 205
    Thank you for your reply

    The compiler is giving the same error
    error C2440: 'initializing' cannot convert from 'char(*)[17]'to'char **'
  • LundinLundin Member Posts: 3,711
    There are three valid ways to dynamically allocate multi-dimensional arrays in C++. The most common is pointer-to-pointer:

    [code]int** array;
    array = new int*[X];
    for(i=0; i<X; i++)
    {
    array[i] = new int[Y];
    }

    ...

    for(i=0; i<X; i++)
    {
    delete [] array[i];
    }
    delete [] array;[/code]

    The advantage is that pointer-to-pointer is very flexible. You could for example write functions for them, accepting all kinds of arrays no matter dimensions. The disadvantage is that the arrays aren't allocated at the same place in memory. So forget everything about memcpy() and compatibility with statically allocated arrays if you use this method.



    Less common are array pointers, because they have weird syntax:

    [code] typedef int (*ArrPtr)[5];
    ArrPtr* array;
    array = new ArrPtr[3];

    (*array)[x][y]= ... ; /* access the array */

    delete [] array;[/code]

    Array pointers have the advantage of being compatible with statically allocated arrays. The syntax is rather painful though.

    The third way is to "mangle" the array into one single, coherent chunk of memory:

    [code] int* array;
    array = new int[3*5]
    ...
    delete [] array;[/code]

    This makes access times a bit longer, but arrays like this can be handy when you are doing matrix maths, rotating values in memory etc. These arrays are naturally also compatible with statically allocated arrays.
  • LundinLundin Member Posts: 3,711
    Also, this isn't valid string handling:

    Arr[0][1]="Basion";

    You need to use C++ strings or learn how C strings work.
  • LundinLundin Member Posts: 3,711
    Neither is correct. See my reply below for correct ways to allocate multi-dimensional arrays.
  • RicardoFurtadoRicardoFurtado Member Posts: 205
    Thank you for your help. the problem was solved with a structure and an array of structs.

    Thank you, once again
Sign In or Register to comment.