Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: my pointer's value is the same when referencing

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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 .

    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;
     
    	}
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: my pointer's value is the same when referencing

    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.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: my pointer's value is the same when referencing

    should be 8
    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:
    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 ..

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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[]..........




    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;
     
    	}
     
     
     
    }
     
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: my pointer's value is the same when referencing

    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.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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





    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);

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default 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.

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default 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.

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: my pointer's value is the same when referencing

    the value when gindex is created is 0;

  12. #12
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default 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:
    Where is the variable created? Add a println there to show its value when it is created.

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: my pointer's value is the same when referencing

    Glad you got it.

  16. The Following User Says Thank You to Norm For This Useful Post:

    jack_nutt (July 6th, 2011)

Similar Threads

  1. Excel Column Letter Referencing
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 21st, 2010, 03:47 PM