This is my first foray into game programming, I'm confused and this has been bugging me for over a week now.
I've managed to get a breakout style clone working, with one level and fixed block positions. I'm trying to work out how to do multiple levels with different numbers/placements of bricks and to my mind, I'll need to play with pointers and dynamically allocating memory.
This example is just to try and prove repointing, then I'll deal with the array side of things for multiple bricks
To prove a point, I've tried creating a pointer and passing it to a function, which "to my understanding" repoints
the pointer and effectively changes its value. The new address/value seems to change within the function but
returns back to the original value in main() as though the new value goes out of scope after the function finishes.
I've read many examples, but they all focus on repointing pointers within a main() function.
Thanks in advance for any help offered
The printf statements give me this:
Address Value Value Address
Before changing value 134520840 20 134520840
Address *topointto 134520856 5 134520856
Before in function 134520840 20 134520840
After in function 134520856 5 134520856
After changing value 134520840 20 134520840
void repoint_existing_pointer(int *intp)
{
int *topointto = new int;
*topointto = 5;
printf("Address *topointto %15i %15i %15i\n", topointto, *topointto, &*topointto);
printf("Before in function %15i %15i %15i\n", intp, *intp, &*intp);
intp = topointto; // try and repoint the pointer to the address containing 5
printf("After in function %15i %15i %15i\n", intp, *intp, &*intp);
}
int main()
{
printf(" %15s %15s %15s\n", "Address", "Value", "Value Address");
int *intpointer = new int;
*intpointer = 20;
printf("Before changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
repoint_existing_pointer(intpointer);
printf("After changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
return 0;
}
I've managed to get a breakout style clone working, with one level and fixed block positions. I'm trying to work out how to do multiple levels with different numbers/placements of bricks and to my mind, I'll need to play with pointers and dynamically allocating memory.
This example is just to try and prove repointing, then I'll deal with the array side of things for multiple bricks
To prove a point, I've tried creating a pointer and passing it to a function, which "to my understanding" repoints
the pointer and effectively changes its value. The new address/value seems to change within the function but
returns back to the original value in main() as though the new value goes out of scope after the function finishes.
I've read many examples, but they all focus on repointing pointers within a main() function.
Thanks in advance for any help offered
The printf statements give me this:
Address Value Value Address
Before changing value 134520840 20 134520840
Address *topointto 134520856 5 134520856
Before in function 134520840 20 134520840
After in function 134520856 5 134520856
After changing value 134520840 20 134520840
void repoint_existing_pointer(int *intp)
{
int *topointto = new int;
*topointto = 5;
printf("Address *topointto %15i %15i %15i\n", topointto, *topointto, &*topointto);
printf("Before in function %15i %15i %15i\n", intp, *intp, &*intp);
intp = topointto; // try and repoint the pointer to the address containing 5
printf("After in function %15i %15i %15i\n", intp, *intp, &*intp);
}
int main()
{
printf(" %15s %15s %15s\n", "Address", "Value", "Value Address");
int *intpointer = new int;
*intpointer = 20;
printf("Before changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
repoint_existing_pointer(intpointer);
printf("After changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
return 0;
}