error while displaying array of pointers to pointers

Why can't I display the array of pointers in the 'display' function, when it worked in the 'position' function,


\****************MAIN********************\

#include <iostream>
using namespace std;

#include "Table.h"

int main()
{
Table *ptrtoF;
int length, width;
char fig1;
cout << "Give any charachter: "<<endl;
cin>>fig1;
cout << "\n0-7 for the position on the table ";
cin>> length ;
cout << "\n0-7 for the position on the table : ";
cin>> width;
ptrtoF->position(fig1,length, width);
ptrtoF->display();

system("pause");
return 0;
}



\************ HEADER FILE**************\
#include <iostream>
using namespace std;
char **table[8][8];
char character='X';
class Table
{

public:
void position(char s, int a , int b)
{

char * temp;
char * temp1;
temp=&character;

cout <<endl<<*temp<<endl;

for(int i=0;i!=8;i++)
{for(int s=0;s!=8;s++)
{
table[i][s]=&temp;}}

temp1=&s;
table[a][b] =&temp1;

for(int i=0;i!=8;i++)
{for(int s=0;s!=8;s++)
{cout << **table[i][s]<<(s==7?"\n":" ") ;}}} //here it works

int display()
{

for(int i=0;i!=8;i++)
{for(int s=0;s!=8;s++)
cout <<"here it stops working: ERROR"<<**table[i][s]<<(s==7?"\n":" ");}} // here it doesn't

};



\****\
it should be a table where you put some char in the position you want , and than displays it when you ask for, but it only works in the position function. I know it can be Simpler ,even not using pointers at all , but I need it for some other thing I'm working at and naturally and most importantly I want to learn it for next time.

I really appreciate your generous help, which cplusplus members every time offer.

uhm not sure if this is the issue but do you notice that your position function takes in a char s but also declares an int s in its body. temp1=&s; <-- which s do you think this is using char s or int s. If it uses int s the variable temp1 is pointing to gets deleted once you exit the function scope which will cause errors in display()
In the position function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	void position(char s, int a , int b)
	{

		char * temp;//local variable
		char * temp1;
		temp=&character;

		cout <<endl<<*temp<<endl;

		for(int i=0;i!=8;i++)
		{
			for(int s=0;s!=8;s++)
			{
				table[i][s]=&temp;//error  - assigning address of local variable
			
			}
		}


You are filling up the the table with the address of a variable (temp) which is
is local to the position function - this is why it all works when inside the position function, but
the variable (temp) will be destroyed when the position function finishes so it's address is no longer valid.
Thanks spaggy and guestgulkan it works yet i declared them global and called 's' by reference, works tip top.

void position(char &s, int a , int b)
Topic archived. No new replies allowed.