first, thanks to the website and the community, u helped me a lot :-)
my problem: I want to use 1 array for some issues: save memory.
I have to run -1 again to use the main function.. why? and it changes the value of first value of array.. how can I fix this?
I know how to fix the value problem with a patch, but I want to fix/understand the main/basic problem. And i don't want to type the -1 twice.
#include<iostream>
usingnamespace std;
struct ArrData { //to return pointer AND size of array (2 returns of function)
double* Ptr;
int Max;
};
ArrData ArrFunc();
ArrData ArrFunc() {
int i = 0;
double fill = 0;
staticdouble arr[30];
static ArrData AD; //sum of returns in struct, size and pointer of array
AD.Ptr = arr;
while (fill != -1) { // fill array until -1
cin >> fill;
arr[i] = fill;
i++;
}
for (int j = 0; j < i; j++) { //searching for bug - until here everything working well
cout << (AD.Ptr + j) << " -> " << *(AD.Ptr + j) << endl;
}
cout << "Test AD.Ptr result: " << *AD.Ptr << endl; //show me fine answer first, after the buggy needed -1 again it is activated again and write -1 in array, so first fault is here in the function i think
AD.Max = (i-1); //-1 to ignore the -1 in arry
return AD;
}
int main() {
int i, MAX = ArrFunc().Max;
double * p = ArrFunc().Ptr;
for (i = 0; i < MAX; i++) { //here i need to insert -1 again... why??? and get bad/wrong results too
cout << (p + i) << " -> " << *(p + i) << endl; // print adress of pointer and value
}
for (i = 0; i < MAX; i++) { //working fine too
*(p + i) = i-1;
cout << "Neuer Wert: " << (p + i) << " -> " << *(p + i) << endl;
}
return 0;
}
while (fill != -1) <-------- comparison of a double to an exact value can be buggy. You might get fill really being -0.9999999999999999999988 instead, for example.
consider changing the code to do this some other way, whether that is to read in a string, check the string value, and convert it to a double to load it, or whatever other approach seems useful.
This is probably not your main issue, but something else to consider and be aware of.
#include<iostream>
usingnamespace std;
struct ArrData { //to return pointer AND size of array (2 returns of function)
int* Ptr;
int Max;
};
ArrData ArrFunc();
ArrData ArrFunc() {
int fill = 0;
int i = 0;
staticint arr[30] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
static ArrData AD; //sum of returns in struct, size and pointer of array
AD.Ptr = arr;
cout << "Test AD.Ptr result: " << *AD.Ptr << endl; // this thing got printed twice.., somehting is repeating itself here..
AD.Max = (i-1); //-1 to ignore the -1 in arry
return AD;
}
int main() {
int i, MAX = ArrFunc().Max;
int * p = ArrFunc().Ptr;
for (i = 0; i < MAX; i++) { //here i need to insert -1 again... why??? and get bad/wrong results too
cout << (p + i) << " -> " << *(p + i) << endl; // print adress of pointer and value
}
for (i = 0; i < MAX; i++) { //working fine too
*(p + i) = i-1;
cout << "new value: " << (p + i) << " -> " << *(p + i) << endl;
}
return 0;
}
i found the problem, can u help me to solve it?
1 2 3
in Main:
int i, MAX = ArrFunc().Max;
int * p = ArrFunc().Ptr;
to get Max and pointer the function works twice, how can i get the values without repeating?