Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
2/20/2006 5:13:57 AM EDT
Can someone look at this code and tell me where the Stack Overflow is? This is homework i had to do but i cant figure out why i keep getting a stack overflow error. Basically its supposed to fill the array with the number 1 using a pointer.

#include <iostream>
using namespace std;

void main()
{
int iArray[20];
int *pFill;
pFill = &iArray[20];

*pFill = 1;

for (int count = 0; count < 20; count++)
cout << iArray[20] << endl;

//system("pause");
}
2/20/2006 10:15:05 AM EDT
[#1]
in case anyone was wondering, here is the solution i cam up with. Dont know why it took me so long to figure it out. lol.

#include <iostream>
using namespace std;

void main()
{
int iArray[20];
int iNumber = 1;
int* pFill;  // creates a pointer variable called pFill

pFill = &iNumber;  // points pFill to the memory location of iNumber

for (int count = 0; count < 20; count++)
iArray[count] = iNumber;  // fills the array with the contents of iNumber

for (int count = 0; count < 20; count++)
cout << iArray[count] << endl;  // prints the data contained within the array

system("pause");
}
2/20/2006 4:58:13 PM EDT
[#2]
pFill isn't really needed in the second one, it isn't used at all
it's been so long since I did anything in C++ in general, and even longer with pointers that that's about all i can add

I need to start coding again, especially since I found a flaw in one of my programs that I was sure worked fine on win98se but is now acting up in XP