Pages: [1]   Go Down
Author Topic: array of array variable names  (Read 716 times)
0 Members and 1 Guest are viewing this topic.
Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I would like to create an array with arraynames. I've already read something about pointers in another topic. That code seems to work good with ints. However with array I get this error:

error: cannot convert 'int (*)[1]' to 'int*' in initialization

Code:
int categorie1[]  = {1 };
int categorie2[]  = {1, 2 };
int categorie3[]  = {3, 4, 5 };
int categorie4[]  = {4, 8, 3, 11 };
int categorie5[]  = {2, 4, 8, 3, 15 , 11, 12, 13 };
int categorie6[]  = {1, 2, 8, 12, 6 };
int categorie7[]  = {20, 1, 15 };
int categorie8[]  = {2, 4, 8 };
int categorie9[]  = {2, 6 };
int categorie10[] = {2 };

int * categorieArray[10] = { &categorie1, &categorie2, &categorie3, &categorie4 ,&categorie5, &categorie6, &categorie7, &categorie8, &categorie9, &categorie10 };

I would like to loop through the array in this way.
Code:
for (int i=0; i < 10; i++)
  { Serial.println(sizeof(categorieArray[i]) / sizeof(int));    
  }

Is it possible what I want ?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The categorie1 variable is an array of ints. Arrays are pointers.

The categorieArray variable is an array of pointers to ints. You need to initialize the array with pointers, not pointers to pointers.

Remove all the & in the initialization section.

By the way, the compiler ignores all white space, so this:
Code:
int * categorieArray[10] = {categorie1,
                            categorie2,
                            categorie3,
                            categorie4,
                            categorie5,
                            categorie6,
                            categorie7,
                            categorie8,
                            categorie9,
                            categorie10 };
is equivalent to the long line you posted, and does not require scrolling back and forth to read.
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

categorieNN are already pointers, there is no sense in making an additional level of references... Just ommit the &
« Last Edit: August 28, 2010, 09:01:52 am by mpeuser » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to loop through the array in this way.
Code:
for (int i=0; i < 10; i++)
  { Serial.println(sizeof(categorieArray[i]) / sizeof(int));    
  }
I assume that you are trying to print out the sizes of the respective arrays categorie1, categorie2, etc.  

After understanding and fixing the part about what kind of pointer you are dealing with (so that it will compile without errors), the bottom line is that what you are trying to do here simply won't (can't, never has, never will) work.  Not in C or C++.  (But: See Footnote.)


Here's why:

For values of i = 0, 1, ... , 9, categorieArray is a pointer.

The size of a pointer is fixed by the compiler implementer.  The size of the pointer has nothing to do with the size of the array.  Period.  Full stop.


Regards,

Dave

Footnote:
To perform something similar to what I think you want, you can do something like the following:

1. Define a struct that has an integer variable holding the size of an array and a pointer to int, which will point to an array.

2. Declare an array of those structs and initialize each struct member with the size of an array and a pointer:

Code:
struct Foo
{
    int siz;
    int * array;
};
int categorie1[]  = {1};
int categorie2[]  = {1, 2};
int categorie3[]  = {3, 4, 5};
int categorie4[]  = {4, 8, 3, 11};
int categorie5[]  = {2, 4, 8, 3, 15 , 11, 12, 13};
int categorie6[]  = {1, 2, 8, 12, 6};
int categorie7[]  = {20, 1, 15};
int categorie8[]  = {2, 4, 8};
int categorie9[]  = {2, 6};
int categorie10[] = {2};

Foo foo[10] = {
    {sizeof(categorie1)/sizeof(int),  categorie1},
    {sizeof(categorie2)/sizeof(int),  categorie2},
    {sizeof(categorie3)/sizeof(int),  categorie3},
    {sizeof(categorie4)/sizeof(int),  categorie4},
    {sizeof(categorie5)/sizeof(int),  categorie5},
    {sizeof(categorie6)/sizeof(int),  categorie6},
    {sizeof(categorie7)/sizeof(int),  categorie7},
    {sizeof(categorie8)/sizeof(int),  categorie8},
    {sizeof(categorie9)/sizeof(int),  categorie9},
    {sizeof(categorie10)/sizeof(int), categorie10},
};

void setup()
{
    Serial.begin(9600);

    for (int i = 0; i < 10; i++) {
        Serial.print("Number of elements in the array of foo[");
        Serial.print(i);        Serial.print("] = ");
        Serial.println(foo[i].siz);
    }
}
void loop()
{
}

Output:

Number of elements in the array of foo[0] = 1
Number of elements in the array of foo[1] = 2
Number of elements in the array of foo[2] = 3
Number of elements in the array of foo[3] = 4
Number of elements in the array of foo[4] = 8
Number of elements in the array of foo[5] = 5
Number of elements in the array of foo[6] = 3
Number of elements in the array of foo[7] = 3
Number of elements in the array of foo[8] = 2
Number of elements in the array of foo[9] = 1

« Last Edit: August 28, 2010, 01:20:02 pm by davekw7x » Logged

Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to all for the tips. Thanks Dave for giving the idea of putting all the sizes in a separate array. In the end I did it without defining a struct.

Code:
int categorie1[]  = {1 };
int categorie2[]  = {1, 2 };
int categorie3[]  = {3, 4, 5 };
int categorie4[]  = {4, 8, 3, 11 };
int categorie5[]  = {2, 4, 8, 3, 15 , 11, 12, 13 };
int categorie6[]  = {1, 2, 8, 12, 6 };
int categorie7[]  = {20, 1, 15 };
int categorie8[]  = {2, 4, 8 };
int categorie9[]  = {2, 6 };
int categorie10[] = {2 };

int * categorieArray[10] = { categorie1, categorie2, categorie3,
                             categorie4, categorie5, categorie6,
                             categorie7, categorie8, categorie9,
                             categorie10 };

int categorieSizeArray[] =
{ sizeof(categorie1) / sizeof(int),
  sizeof(categorie2) / sizeof(int),
  sizeof(categorie3) / sizeof(int),
  sizeof(categorie4) / sizeof(int),
  sizeof(categorie5) / sizeof(int),
  sizeof(categorie6) / sizeof(int),
  sizeof(categorie7) / sizeof(int),
  sizeof(categorie8) / sizeof(int),
  sizeof(categorie9) / sizeof(int),
  sizeof(categorie10) / sizeof(int),
};

void setup()
{ Serial.begin(57600);
  
  for (int i=0; i < 10; i++)
  { // print category number
    Serial.print("category :");
    Serial.println(i+1);
    
    // print the contents of each category
    for (int j=0; j < categorieSizeArray[i]; j++)
    { Serial.println(categorieArray[i][j]);
    }  
  }
  
}


void loop()
{  
}


Logged

Pages: [1]   Go Up
Jump to: