| View previous topic :: View next topic |
| Author |
Message |
stuarte9
Joined: Mon Mar 08, 2010 5:03 pm Posts: 31 Location: Scotland
|
Posted: Thu Jan 12, 2012 2:37 pm Post subject: Need help with a segmentation fault |
|
|
Hi all,
I'm teaching myself C++ and getting a segmentation fault with my latest small prog. The fault occurs within the following function, at the "std::cin" statement.
| Code: | void getValues(int** theMatrix, int& theRows, int& theColumns)
{
for (int row = 0; row < theRows; ++row)
{
// prompt for values
std::cout << std::endl << "Enter values for row " << (row + 1) << std::endl;
// and then use a for loop to assign values from cin
for (int column = 0; column < theColumns; ++column)
{
std::cin >> theMatrix[row][column];
}
}
}
|
The intention of this function is to prompt for and read integer values into a 2D array.
Could someone please explain why I'm getting this fault as everything seems OK to me.
Thanks in advance,
Stuart |
|
| Back to top |
|
 |
stuarte9
Joined: Mon Mar 08, 2010 5:03 pm Posts: 31 Location: Scotland
|
Posted: Fri Jan 13, 2012 2:27 pm Post subject: Solved! |
|
|
Hi all,
Just a quick note to say I've now solved the problem outlined in my original posting. It boiled down to not putting an ampersand (&) at the beginning of variable "theMatrix" in the function header.
Stuart |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|