my pointer's value is the same when referencing
the code simply stores dataInterest type objects into the dlist[] of nodeobj[1] and nodeobj[2]
I want to create gptr for each nodeobj[]'s dlist[] .so that i can refer to it as nodeobj[1].dlist[0].gptr.value and nodeobj[2].dlist[0].gptr.value........but the value for both these pointers comes out to be the same,although i tried checking for nodeobj[1].dlist[0] and nodeobj[2].dlist[0] ,both of them are different .
Code Java:
public class mhand {
public static node[] nodeobj;
public static void main(String[] args) {
boolean onetime=true;
nodeobj=new node[5];
nodeobj[0]=new node(1,2);
nodeobj[1]=new node(6,6);
nodeobj[2]=new node(2,9);
nodeobj[0].nlist[0]=nodeobj[1]; //putting into node 0's neigbor list node 1 and 2
nodeobj[0].nlist[1]=nodeobj[2];
mhand m=new mhand();
while(true)
{
if(onetime) //for storing dobj into dlist
{
packet pobj=new packet(3,0);
m.n2pkt(pobj);
onetime=false;
nodeobj[2].dlist[0].gptr.value=8; //fixed value
}
nodeobj[1].dlist[0].gptr.value++; //incrementing value fo one pointer
m.call();
}}
public void n2pkt(packet pobj) // for making dobject and puting into dlist
{
dataInterest dobj=new dataInterest(pobj.type,pobj.senderid) ;
for(int i=0;i<5;i++)
{
if((nodeobj[0].nlist[i]!=null)){
nodeobj[0].nlist[i].pkt[0]=pobj;
nodeobj[0].nlist[i].dlist[0]=dobj;
nodeobj[0].nlist[i].iFlag=true;
System.out.println("printing for index "+i);
}
}
}
public void call() //prints pointer values
{
System.out.println("print for node 1 "+nodeobj[1].dlist[0].gptr.value);
System.out.println("printing for node 2 "+nodeobj[2].dlist[0].gptr.value);
}
}
public class node {
boolean iFlag=false;
int x;
int y;
node nlist[]=new node[5];
public dataInterest[] dlist=new dataInterest[5];
dataInterest ptr=new dataInterest();
packet[] pkt=new packet[1];
public node(){}
public node(int a,int b)
{
x=a;
y=b;
}
}
public class dataInterest {
public Gradient[] myglist=new Gradient[5];
int value=0;
int type;
int id;
public Gradient gptr=new Gradient();
public dataInterest(int v,int c)
{
type=v;
id=c;
}
public dataInterest(){}
}
public class Gradient {
int id;
int value=0;
public Gradient(int a)
{
id=a;
}
public Gradient()
{
}
}
public class packet {
int senderid;
int type;
public packet(int a,int b)
{
type=a;
senderid=b;
}
}
Re: my pointer's value is the same when referencing
Quote:
the value for both these pointers comes out to be the same,
Add some printlns to show the values of those variables as they are being set and as they are referenced.
What is the problem with the values being the same? Many variables can have the same value as other variables.
Re: my pointer's value is the same when referencing
The problem is that i hardcoded nodeobj[2].dlist[0].gptr.value=8; but the count starts with 8 from both pointers
node zero has coor 1 2
node 1 has coor 6 6
node 1 has coor 3 1
node zero has coor 1 2
node 1 has coor 6 6
printing for index 0
printing for index 1
print for node 1 9
printing for node 2 9 ...........................>should be 8
print for node 1 10
printing for node 2 10 ...........................>should be 8
print for node 1 11
printing for node 2 11
Re: my pointer's value is the same when referencing
I don't see any printouts in your post that shows where the value was set to 8.
In fact there don't appear to be any printouts for any time the value of the variable was changed.
I suggested:
Quote:
show the values of those variables as they are being set
Then your printout could show:
...
Set node 2 to 8
...
set node 2 to 9 <<<<< Here would be the problem if the value should be left at 8
...
printing for node 2 9 ..
Re: my pointer's value is the same when referencing
Norm i have changed my code,instead of making ptr and gptr objects,i am now using dindex and gindex of 'int' type.
Still i am getting the problem that
if i use nodeobj[0].dindex and nodeobj[1].dindex they are different ,like each node has a unique index(which i will use to point index of array to store dat)... but when i use nodeobj[1].dlist[0].gindex and nodeobj[1].dlist[1].gindex the values of gindex are same even when i set one to 8 nodeobj[1].dlist[0].gindex=8; ,i will use gindex to point to myglist[] next location , for each object stored in dlist[]..........
Code :
public class mhand {
public static node[] nodeobj;
public static void main(String[] args) {
boolean onetime=true;
nodeobj=new node[5];
nodeobj[0]=new node(1,2);
nodeobj[1]=new node(6,6);
nodeobj[2]=new node(2,9);
nodeobj[3]=new node(1,2);
nodeobj[4]=new node(6,6);
mhand m=new mhand();
while(true)
{
if(onetime)
{
packet pobj=new packet(3,0);
m.n2pkt(pobj);
onetime=false;
nodeobj[1].dlist[0].gindex=8;
}
nodeobj[1].dlist[1].gindex++;
m.call();
}}
public void n2pkt(packet pobj)
{
dataInterest dobj=new dataInterest(pobj.type,pobj.senderid) ;
nodeobj[1].dlist[0]=dobj;
nodeobj[1].dlist[1]=dobj;
}
public void call()
{
System.out.println("gindex value for node 1 dlist[0]"+nodeobj[1].dlist[0].gindex);
System.out.println("gindex value for node 1 dlist[1] "+nodeobj[1].dlist[1].gindex);
}
public class node {
boolean iFlag=false;
int x;
int y;
node nlist[]=new node[5];
public dataInterest[] dlist=new dataInterest[5];
int dindex;
packet[] pkt=new packet[1];
public node(){}
public node(int a,int b)
{
x=a;
y=b;
}
}
public class dataInterest {
public Gradient[] myglist=new Gradient[5];
int type;
int id;
int gindex;
public dataInterest(int v,int c)
{
type=v;
id=c;
}
public dataInterest(){}
}
public class Gradient {
int id;
public Gradient(int a)
{
id=a;
}
public Gradient()
{
}
}
public class packet {
int senderid;
int type;
public packet(int a,int b)
{
type=a;
senderid=b;
}
}
}
Re: my pointer's value is the same when referencing
Quote:
when i use
nodeobj[1].dlist[0].gindex
and
nodeobj[1].dlist[1].gindex
the values of gindex are same
Does your code set them to have the same values?
I'll suggest it again, add printlns to your code to display the values of the variables every time they are set. The printout should show you where you are giving them the same value.
Re: my pointer's value is the same when referencing
In this method if i change the degree of incrementing gindex for different dlist[] ,the output printed is same ....
Console output:
gindex value for node 1 dlist[0] 3
gindex value for node 1 dlis[1] 3
Code :
public void call()
{
nodeobj[1].dlist[1].gindex++;
nodeobj[1].dlist[1].gindex+=2;
System.out.println("gindex value for node 1 dlist[0] "+nodeobj[1].dlist[0].gindex );
System.out.println("gindex value for node 1 dlis[1] "+nodeobj[1].dlist[1].gindex);
Re: my pointer's value is the same when referencing
If you change the value of a variable via one path and it changes the value of the variable that is on another path, what does that imply? There is only ONE variable. All the different paths point to the same variable.
Where is the variable created? Add a println there to show its value when it is created.
Re: my pointer's value is the same when referencing
Correction in the previous thread i meant
nodeobj[1].dlist[0].gindex++; // not dlist[1]
nodeobj[1].dlist[1].gindex+=2;
Norm i want to make gindex different for each dlist[]'s index so that each nodeobj's dlist's index has differnt value,how can i do that...
how can i modify so that
nodeobj[1].dlist[0].gindex
nodeobj[1].dlist[1].gindex have different values
Re: my pointer's value is the same when referencing
Do you know why the value is not different?
If you understand that, you will be able to see a way to change it so each is different.
Re: my pointer's value is the same when referencing
the value when gindex is created is 0;
Re: my pointer's value is the same when referencing
No actually i did not get why the value is not different ,a lil insight might help
thanks
Re: my pointer's value is the same when referencing
Did you add the println statements to EVERY where that gindex gets a value?
That includes in the constructor of the class where the object with the gindex variable is created?
In post #8 I asked you:
Quote:
Where is the variable created? Add a println there to show its value when it is created.
Re: my pointer's value is the same when referencing
Norm i figured out what was wrong,i want assigning dobj to both nodeobj.dlist so it was refering to same variables..
i have put different object in dlist and its ok now,thanks
Re: my pointer's value is the same when referencing