Later on I will have my own datatype which holds different values (name, size, datarray,...). But the array values I entered "dissapear". It looks more like the wrong address is called (completly nonsense values).
Datastorage DATA;
void myFancyFunction(DATA) {
// do some stuff with DATA
}
void loop() {
myFancyFunction(DATA);
// still use all the information held by DATA
}
My datastorage looks like this
class Datastorage {
Datastorage(char*);
void setResults(int*);
int* getResults();
int SIZE = -1;
int* RESULTS;
char* NAME;
}
Your array is associated ONLY with that function. When the function is entered, the address of the array is established. When the function is exited, the address disappears and a new address is established when the function is entered next time.
Define the array at the beginning of the program so it is fixed in location.
Paul_KD7HB:
Your array is associated ONLY with that function. When the function is entered, the address of the array is established. When the function is exited, the address disappears and a new address is established when the function is entered next time.
Similarly, the statement:
x = temp;
doesn't do what you think it does either. It only modifies the local copy of the passed argument (on the stack). It doesn't change the value of 'x' in the calling function (loop).
I tried the suggestion of Paul and defined the pointer outside the functions. Same results.
Why should the function care where the array is defined? The address is given at the moment its created. When I pass it as pointer to any function should the address not be the same?
I would like to have my own datatype DATASTORAGE which holds all kind of information of a chip I'm working on. This information (int* RESULTS) changes when the chip is called.
I want to write data into the DATASTORAGE and pass the variable to some functions.
The data inside the DATASTORAGE is correct (printing inside the seter Method). When the data is called upon later on it looks like the wrong address (SIZE and NAME are still correct).
I have now my custom datatype, can fill it with data from another function (my chip) and use it outside this function (in other functions or pass it along).
Now that I see it, it kind of makes sense (until it stops behaving like I want it to)
Is this the right (elegant) way to do what I'm trying to do?
v3xX:
Is this the right (elegant) way to do what I'm trying to do?
"Convoluted" is the term I'd use.
You didn't elaborate on the details of your "chip" DATASTORAGE, but it seems reasonable to use a 'struct' for it. I would keep that struct private within the class and not pass pointers to it around.
@gfvalvo: I played around with your suggestion and used a 'Struct' definition across multiple files. Also I modified the Chip-Class and the other classes which use the chipdata (now as Struct). I also tried to streamline the passing of the arguments.
I think the whole Pointer and addressing business is clear for now, until the next seemingly easy problem arrives.
Thanks for the help.