I have a problem where I cannot print out the pointer outside the function.
This is what I have in my int main(), where I've created 2 pointers to pass into the function extractRange.
if(i == 0)
{
int *x, *y;
extractRange(grid[i], x, y);
cout << x << endl << y << endl; // will cout the address
cout << *x << endl << *y; // crash here and show segmentation fault (core dumped) error
}
This is my extractRange function:
void extractRange(string line, int *start, int *end)
{
int x, y;
int pos = 0;
for (; pos < line.length(); pos++ )
{
if ( isdigit(line[pos]))
break;
}
line = line.substr(pos, line.length() - pos);
x = atoi(line.c_str());
pos = line.find("-");
line = line.substr(pos + 1);
y = atoi(line.c_str());
start = &x;
end = &y;
cout << *start << endl; // able to cout the int 0
cout << *end << endl; // able to cout the int 8
}
How come I am able to cout out the value of start and end inside the function but not outside the function?