Arduino: cannot get value from array of struct - arrays
I have a bidimensional array of struct that I defined and I want to get a value in a struct which itself is contained in an array.
I have tried to do the same kind of code in bare C and it seems to work while or Arudino IDE it doesn't (code isn't the exact same but it's generally the same thing).
Look at the example below, I am printing an arbitrary value taken out of my array of struct over serial and the only input I get on the Serial Monitor is a suit of 'ΓΏ' which from what I've seen on the internet means that the serial buffer is empty.
Does anything look wrong to you within the code below? (look at the end to see the relevant code)
#include <Wire.h>
typedef struct {
char nom[17]; // wanted number +1 for terminaison
byte cor1[2]; // correspondance
byte cor2[2];
byte cor3[2];
byte cor4[2];
byte i2c;
byte led;
} Station;
Station lignes[14][15] = {{
{"Champs Elysees", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11101111},
{"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
{"Tuileries", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11101111},
{"Palais Royal", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B10111111},
{"Louvre Rivoli", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11101111},
{"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
{"Hotel de Ville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11101111},
{"St-Paul", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
{"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Ledru-Rollin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Faidherbe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011}
},
{
},
{
{"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
{"Havre Caumartin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111110},
{"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
{"Quatre Septembre", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111101},
{"Bourse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11011111},
{"Sentier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11110111},
{"R. Sebastopol", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111101},
{"Arts et Metiers", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B01111111},
{"Temple", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B10111111},
{"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111}
},
{
{"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
{"R. Sebastopol", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111101},
{"Etienne Marcel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111011},
{"Les Halles", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B10111111},
{"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
{"Cite", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B01111111},
{"St-Michel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
{"Odeon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"St-Germain", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B10111111},
{"Saint-Sulpice", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
{"St-Placide", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
{"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"Vavin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
{"Raspail", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Denfert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
{"J. Bonsergent", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111011},
{"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
{"Oberkampf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B01111111},
{"Richard Lenoir", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
{"Breguet Sabin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Quai de la Rapee", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"Gare Austerlitz", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"Saint Marcel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
{"Campo Formio", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
},
{
{"Pasteur", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
{"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"Edgar Quinet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
{"Raspail", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Denfert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
{"Le Peletier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11110111},
{"La Fayette", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111011},
{"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
{"Pyramides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B01111111},
{"Palais Royal", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B10111111},
{"Pont Neuf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11011111},
{"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
{"Pont Marie", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"Sully Morland", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
{"Jussieu", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Place Monge", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"C. Daubenton", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Les Gobelins", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
},
{
{"Invalides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11110111},
{"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
{"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
{"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
{"Richelieu Drouot", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111101},
{"Gds Boulevards", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111011},
{"Bonne Nouvelle", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111110},
{"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
{"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
{"F. du Calvaire", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11101111},
{"St-S. Froissart", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B01111111},
{"Chemin Vert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
{"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Ledru Rollin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Faidherbe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011}
},
{
{"Saint Philippe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11110111},
{"Miromesnil", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111011},
{"Saint-Augustin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111101},
{"Havre Caumartin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111110},
{"La Fayette", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111011},
{"Richelieu Drouot", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111101},
{"Gds Boulevards", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111011},
{"Bonne Nouvelle", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111110},
{"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
{"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
{"Oberkampf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B01111111}
},
{
{"Duroc", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Vanneau", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
{"Sevres Babylone", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Mabillon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B01111111},
{"Odeon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"La Sorbonne", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
{"Maubert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
{"Cardinal Lemoine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
{"Jussieu", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Gare Austerlitz", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
{"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
{"Hotel de Ville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11101111},
{"Rambuteau", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11011111},
{"Arts et Metiers", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B01111111},
{"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
{"Goncourt", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111101},
{"Belleville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111110}
},
{
{"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
{"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
{"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
{"Assemblee", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111011},
{"Solferino", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111110},
{"Rue du Bac", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11101111},
{"Sevres Babylone", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
{"Rennes", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"N-D. Des-Champs", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
{"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"Falguiere", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
{"Pasteur", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
{"Volontaires", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
{"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
{"Miromesnil", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111011},
{"Champs Elysees", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11101111},
{"Invalides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11110111},
{"Varenne", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111101},
{"St-Fr. Xavier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11011111},
{"Duroc", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
{"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
{"Gaite", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110}
},
{
{"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
{"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
{"Pyramides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B01111111},
{"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
{"Gare de Lyon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111}
}};
void setup() {
// Wire.begin();
Serial.begin(9600);
}
void loop(){
// for (int j=0; j<16; j++) {
byte a = lignes[0][0].i2c;
Serial.write(a);
/* for (int i=0; i<14; i++) {
Serial.print(lignes[0][i].i2c, BIN);
Serial.print(lignes[0][i].led, BIN);
Serial.println(" ");
/* Wire.beginTransmission(lignes[0][i].i2c); // PCF8574P GGG
Wire.write(lignes[0][i].led);
Wire.endTransmission();
delay(10);*/
// }
// }
}
Thanks a lot in advance for your help, I already spent a lot of time on this...
In your given example, you are writing "lignes[0][0].i2c" which is B0100001. That is an ASCII code for space. Try validating your serial conection with a simple
Serial.Write("Hello World");
If that passes then try printing an actual string, for example,
Serial.Write(&lignes[0][0].nom)
Apparently, it was caused by a memory overflow (thanks to Arduino IDE for NOT pointing that).
The Arduino IDE says 36kB available but that's Flash memory, one has to account SRAM.
This is worked-around by using PROGMEM.
cf. http://arduino.cc/en/Tutorial/Memory
http://www.arduino.cc/en/Reference/PROGMEM
Also be careful, you have to use special functions for extracting and using value stored with PROGMEM.
Related
global 2D array values changing between modules
I have a 2D extern array "the_board[17][25]". I am fairly new to C and believe I have some linking issue. in my board.c file I initialized the board, giving every cell a value. when calling the board in my other functions, the values change, even though I have made no edits to the array since initializing it. header file to declared array: #define BOARD_SIZE_X 25 #define BOARD_SIZE_Y 17 extern int the_board[BOARD_SIZE_Y][BOARD_SIZE_X]; #define DA 0 /* disallowed */ #define RE 1 /* red */ #define GR 2 /* green */ #define EM 7 /* empty */ board.c file containing initialization: #include "my_header.h" int the_board[BOARD_SIZE_Y][BOARD_SIZE_X] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7}, {0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0}, {0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0}, {0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0}, {0, 0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0, 0}, {0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0}, {0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0}, {0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0}, {7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; Sorry for the large block of code it has to be manually defined. This is where the problem I run into. When calling the_board[][], the values become screwed up. For an example this is what happens when I print the_board[BOARD_SIZE_Y][BOARD_SIZE_X] in main.c: img of array: https://i.stack.imgur.com/KLQqT.png #include "my_header.h" int main() { int i, j; int the_board[BOARD_SIZE_Y][BOARD_SIZE_X]; for (i = 0; i < BOARD_SIZE_Y; i++){ for (j = 0; j < BOARD_SIZE_X; j++) { printf(" %d ", the_board[i][j]); } } return 0; }
You have one initialised global the_board, defined in board.c. You have one non-initialised local the_board defined inside main(). That second one hides the first one. Delete the line which defines it, i.e. int the_board[BOARD_SIZE_Y][BOARD_SIZE_X];
How to fix "variable-sized object may not be initialized"?
I've created a code to calculate my class notes, it was working the first time, but after some runs, it shows: [Error] variable-sized object may not be initialized I don't know why! :( CODE: system("Color Fs"); float total = 0, moyenne; int i,j, mawad = 11, ghanat = 3; char * NAMES[] = {"Archi", "Dev", "EEJ", "ENG", "AR", "FR", "MATH", "RESEAU", "LINUX", "WIN", "TEC"}; float NOTES[mawad][ghanat] = { {20, 0, 0}, {20, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {15, 0, 0}, {35, 0, 0}, {30, 0, 0}, {30, 0, 0}, {10, 0, 0}, }; printf("------------------------------ LAY YSBR LGAMI3 ------------------------------\n"); for (i=0;i<mawad;i++){ printf("DGHL NO9TA DYAL %s", NAMES[i]); for (j=0;j<2;j++) { do { printf("\t\tFRD %d:\n", j+1); scanf("%f", &NOTES[i][j+1]); } while (NOTES[i][j+1] > 20); // ILA DGHL KTR MN 20; } total += ((NOTES[i][1] + NOTES[i][2]) / 2) * NOTES[i][0]; } moyenne = total / 200; printf("\n\tMoyenne: %.3f \n", moyenne);
To fix that we need to add values directly: float NOTES[11][3] = { {20, 0, 0}, {20, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {15, 0, 0}, {35, 0, 0}, {30, 0, 0}, {30, 0, 0}, {10, 0, 0}, };
TCC Error: index too large
I have been playing around with ray-casting and decided to use 3D arrays to make levels, However TCC spits out this: M_MAIN.C:19: error: index too large Is there a limit to how large an array can be? Code: #define MAP_01_WIDTH 8 #define MAP_01_HEIGHT 8 #define MAP_01_DEPTH 8 #define MAP_01_PLAYER_START_X 2 #define MAP_01_PLAYER_START_Y 2 #define MAP_01_PLAYER_START_Z 2 char MAP_01[MAP_01_WIDTH][MAP_01_DEPTH][MAP_01_HEIGHT] = { { {1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {2, 0, 3, 0, 0, 4, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 0, 0, 0, 0, 0, 0, 1}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 0, 0, 0, 0, 0, 2}, {1, 0, 0, 0, 0, 0, 0, 1} }, { {1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1} } };
You define the array with size 8x8x8 but initializing it with 9x8x8 elements. Fix the size with: #define MAP_01_WIDTH 9 Or better yet, let the compiler calculate that: char MAP_01[][MAP_01_DEPTH][MAP_01_HEIGHT] = {...};
error: array type has incomplete element type
There are already a few of those threads on here, but none of them actually answer my problem. In my header I have: struct table_val { char res; char carry; }; typedef struct table_val TABLE_VAL; and in my source file I try to create this static array of structs: struct TABLE_VAL add_table[] = { {0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {7, 0}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {8, 0}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {9, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1} }; Compiling this leads to the error in the title. Am I not allowed to create an array in that manner? If not, is there a short version, than initializing every struct after creation?
Use TABLE_VAL add_table[] or struct table_val add_table[] and not struct TABLE_VAL add_table[] When you do: typedef struct table_val TABLE_VAL; you are creating a type alias named TABLE_VAL for the type struct table_val.
How can I avoid duplicates in ScriptDb?
I have some objects to save in the database, but they must be unique every time the script is executed. These objects are just going to change values every instance of the application. The problem is that when I create the first object, more copies are also created every time the script is executed after that. I try to use the same object again, but when I do the query, I get objects from previous executions giving me the wrong values. I just want to be able to reset the same objects to their default values and not have many unnecessary copies in the ScriptDb. Here is some code: // Get a database instance var database = getDb(); // Create a new employee instance var myEmployee = { element: "currentEmployee", firstName: "", lastName: "", ID: 0, manager: "", managerEmail: "", department: "1 - University Store", startDate: "", endDate: "", regularHours: 0, vacationHours: 0, sickHours: 0, personalHours: 0, H2Hours: 0, overtimeHours: 0, totalHours: 0, rowLocation: 0, salaryType: "H", }; var week1 = { // Identify the week and for query purposes element: "Week1", // User entries HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, // Calculated totals TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0, // Week totals Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0, } var week2 = { // Identify the week and for query purposes element: "Week2", // User entries HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0}, // Calculated totals TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0, // Week totals Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0, } // Save these to the database database.save(myEmployee); database.save(week1); database.save(week2); Then, I would use the statement ScriptDb.getMyDb.query({element: element}).next() to retrieve the object. The string element containing "currentEmployee", "Week1", or "Week2".
To avoid re-creating uniquely-keyed objects in ScriptDb, you must first check whether the object you're thinking of saving is already there. If it is, then you need to get the handle of the stored object to use for updates. If it's not there, you will save() your new object, and the return value will be the handle of the copy in the database. The general pattern is this: // Get a database instance var db = getDb(); var result = db.query( {key:keyval, ...} ); if (result.hasNext()) { // The object already exists in ScriptDb var obj = result.next() } else { // The object doesn't exist in ScriptDb, so create it // This example would start by creating only the key values, // expanding the object during the update stage below. // Alternatively, you could create the entire object with all // its properties. obj = db.save({key:keyval, ...}) } // Can now update the obj that is in ScriptDb obj.prop1 = value1; obj.prop2 = value2; ... // Save updated object db.save(obj); Here's an example of how that pattern could apply to your code. You'll see here that the alternative approach to creating new objects is being used, in which the new object is created in ScriptDb with all its properties at once. // Get a database instance var database = getDb(); // Find or create employee instance var result = database.query({element: "currentEmployee"}); if (result.hasNext()) { var myEmployee = result.next(); } else { myEmployee = database.save({ element: "currentEmployee", firstName: "", lastName: "", ID: 0, manager: "", managerEmail: "", department: "1 - University Store", startDate: "", endDate: "", regularHours: 0, vacationHours: 0, sickHours: 0, personalHours: 0, H2Hours: 0, overtimeHours: 0, totalHours: 0, rowLocation: 0, salaryType: "H" }); }; // Find or create Week1 instance result = database.query({element: "Week1"}); if (result.hasNext()) { var week1 = result.next(); } else { week1 = database.save({ // Identify the week and for query purposes element: "Week1", // User entries HW: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, VH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, SH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, PH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, TH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, // Calculated totals TotalHW: 0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0, // Week totals Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0, }); } // Find or create Week2 instance result = database.query({element: "Week2"}); if (result.hasNext()) { var week2 = result.next(); } else { week2 = database.save({ // Identify the week and for query purposes element: "Week2", // User entries HW: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, VH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, SH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, PH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, TH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0}, // Calculated totals TotalHW: 0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0, // Week totals Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0, }); } // At this point, myEmployee, week1 and week2 refer to the ScriptDb objects. // While we can use them as any javascript object, they also contain information // relevant to their state as ScriptDb objects. ...