Pointer (or value inside it) changing; unclear why

I'm writing a program for project euler (counting sundays) in which I am trying to use pointers to make code as simple as possible. I run into a problem where the value inside the pointer I use suddenly switches to an absurdly large int value which I'm guessing means the location of where the pointer points either changes or the data there gets destroyed.

The first time mswitch executes all is fine; returned pointer points to array with 1 and 0 which is fine.
Second time though it gives very large negative int value and the change happens between the pointer that is returned the first time it executes, and where it gets plugged in into the second instance.

Any reason why this should happen? Sorry for the long post.

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
66
67
68
69
70
71
72
73
74
75
76
 
void printwkDay(int d) //prints easy to read output; 1-7 becomes monday-sunday
{
	switch (d)
	{
	case 1:
		cout << "Monday";
		break;
	case 2:
		cout << "Tuesday";
		break;
	case 3:
		cout << "Wednesday";
		break;
	case 4:
		cout << "Thursday";
		break;
	case 5:
		cout << "Friday";
		break;
	case 6:
		cout << "Saturday";
		break;
	case 7:
		cout << "Sunday";
		break;
	default:
		cout << "Meep";
	}
}




 //mswitch takes a pointer that has 2 values in it, and takes an integer that determines size of the month
int * mswitch(int * ptr, int msize)
{
	int d = 0; //day of the month
	int dw = *ptr - 1; // dw is day of the week, integers from one to 7
	int count = *(ptr + 1); //counting sundays that fall on the first of each month
	cout << "Pointer: " << *ptr << ", " << *(ptr + 1) << "\n"; //show the values in the address
	cout << "Day start: " << dw << "\n"; //day of week that the function starts with
	while (d < msize)
	{
		d++;
		dw++;
		if (dw == 8)
		{
			dw = 1;
		}
		printwkDay(dw);
		cout << " " << d << "\n";
		if (dw == 7 && d == 1)
		{
			dw = 0;
			count++;
		}
		
		
	}
	int arr[2] = { dw, count };
	ptr = arr;
	cout << "Pointer: " << *ptr << ", " << *(ptr + 1) << "\n";
	return ptr; //returns the day of week at the end of the month as well as the current count
}
int main()
{
int arr[2] = { 6, 0 };
	int * x = arr;
	int * y = mswitch(x, 31);
	cout << "RETURNED POINTER: " << *y << ", " << *(y + 1) << "\n";
	x = mswitch(y, 30);
	cout << "RETURNED POINTER: " << *x << ", " << *(x + 1) << "\n";
	keep_window_open();
	return 0;
}
Last edited on
I am trying to use pointers to make code as simple as possible.
The road to hell is paved with good intentions.

Line 64: Returning a pointer to local array.
Think of a local variable like putting a sign in a parking space reading "please don't crush this car". When a function returns, you automatically take the sign away. If you come back later you can expect to find your car, someone else's car, or just a pile of scrap.
Okay, that makes sense. Is there a better way to return these two values? I'm not quite sure how else to return an array.
I would just modify the input array. std::pair also works.
Last edited on
It all depends on where you make the array! This is what's called scope. An example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//This will delete the array when foo goes back into main :O not what we want!
#include <iostream>
using namespace std;

const int SIZE = 8;

int* foo() {
//If lizards is created in this function....
    int lizards[SIZE] = {0};
    for (int i=0; i<SIZE; ++i)
        lizards[i] = i+4;
    return lizards;
}

int main() {
    int* lizards = foo();
/*...and used here, then it will already be deleted and you cannot expect it to be there anymore!*/
    cout << "I touched " << lizards[2] << " lizards today." << endl;
    return 0;
}


any time any kind of variable is created after a "{" it will be deleted at the corresponding "}"

what you wanted to do was this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

const int SIZE = 8;

/*void foo(char *lizards, int size) { /*also works, but char lizards[] tells people that you want
 to pass in an array not just a pointer to an individual int and general clarity is more
 important than personal style :) */

void foo(char lizards[], int size) {
    for(int i=0; i<SIZE; ++i)
        lizards[i] = i+8;
}

int main() {
    char lizards[SIZE] = {0}; /* Lizards is created here meaning it will not be deleted by the
 time we call foo or print it to the screen! */
    foo(lizards, SIZE); //This is how you pass an array to a function.
    cout << "I am as strong as " << lizards[3] << " lizards. Come at me!" << endl;
    return 0;
}
Last edited on
RokSav wrote:
I am trying to use pointers to make code as simple as possible.
Any code involving pointers is inherently less simple than code that does not involve pointers.
http://www.LB-Stuff.com/pointers
Hi RokSav.

One doubt I have.

ptr is a pointer type input parameter, Then why you need to return it.
Means you can disrectly use ptr at the places(line 70/72) where you call that function "mswitch".
Topic archived. No new replies allowed.