access pointer to array in struct

Hello,

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.

Thank you already :-)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
#include<iostream>

using namespace 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;			
	static double 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;
}

Last edited on
double fill = 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.
Last edited on
Thanks for your answer!
I tried some other methods and changed all in integer, for example:
1
2
3
4
5
do {		// fill array until -1
		cin >> fill;
		arr[i] = fill;
		i++;
	} while (i < 10);


i got a feeling my fault is not here :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>

using namespace 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;
	static int 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?
Last edited on
SOLVED..
i worked on this 3 days... lol :-)

here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>

using namespace std;


struct ArrData {		
	int* Ptr;
	int Max;
};

ArrData ArrFunc();

ArrData  ArrFunc() {

	
	int fill = 0;
	int i = 0;
	static int arr[30];
	static ArrData AD;			//sum of returns in struct, size and pointer of array
	AD.Ptr = arr;

	
	do {		// fill array until -1
		cin >> fill;
		arr[i] = fill;
		i++;
	} while (fill != -1);

	for (int j = 0; j < i-1; j++) {							
		cout << (AD.Ptr + j) << " -> " << *(AD.Ptr + j) << endl;
	}
	
	AD.Max = (i-1); //-1 to ignore the -1 in arry
	return AD;
}

int main() {
	
	ArrData x = ArrFunc();   // solved here
	int i;

	for (i = 0; i < x.Max; i++) {								
		cout << (x.Ptr + i) << " -> " << *(x.Ptr + i) << endl;		// print adress of pointer and value

	}
	for (i = 0; i < x.Max; i++) {	
		cout << "new value: " <<  (x.Ptr + i) << " -> " << *(x.Ptr + i)+3 << endl;  //change values

	}


	
	return 0;
}

Topic archived. No new replies allowed.