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.
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;
}
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.
//This will delete the array when foo goes back into main :O not what we want!
#include <iostream>
usingnamespace std;
constint 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 "}"
#include <iostream>
usingnamespace std;
constint 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;
}
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".