How many times a pointer can point to another pointer>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • visualbasic1111
    New Member
    • Nov 2007
    • 27

    #1

    How many times a pointer can point to another pointer>

    Hi All, I want to know whether how how many time a pointer can point to another pointer, in the way

    P1->P2->P3->P4............ ......

    up to where this link can proceed ans is valid in C or C++
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    It is only limited by the memory you have. You could even have circular pointers, creating an infinite loop!
    P1 -> P2 -> P3 -> P4 -> P1 -> ....

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This is a very poor coding techinque. It is called "spaghetti code". By using it you create threads of variables p1->p2->p3->p4 where if aby of the pointers are zero, you program crashes. Where if any of the pointers are members of structs you have exposed the name of the member and have destryed your ability to redesign the struct without trashing trhe entire program. Where the values of the objects pointed at can be changed willy-nilly anywhere in the code so you have to debug 75,000 functions instead of the one that's doing it.

      etc...

      Research encapsulation for ways to avoid this.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Are you looking for ways to obfuscate your code, or are you really looking for some sort of Linked List Implementation?

        Comment

        • visualbasic1111
          New Member
          • Nov 2007
          • 27

          #5
          thanks 4reply but I hear from my friend who is an mca student, that the limit in this way is upto 14 pointers measns
          p1->p2->p3->.............. .......->p13->p14 then I was a bit confused whether what is the limit when a pointer can point to another pointer. Thank You All.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            try this program, compiles and runs for me
            Code:
            #include <iostream>
            
            using namespace std;
            
            struct Tester
            {
            	int value;
            	Tester* next;
            };
            
            static const int LIMIT = 100;
             
            int main()
            {
            	Tester top = {0, NULL};
            	Tester* current = &top;
            	
            	for(int ix = 0; ix<LIMIT; ix++)
            	{
            		current->next = new Tester;
            		if (current->next == NULL)
            		{
            			break;
            		}
            		
            		current = current->next;
            		current->value = ix+1;
            		current->next = NULL;
            	}
            
            	cout << top.next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->
            	next->next->next->next->next->next->next->next->next->next->value
            	<< endl;
            }
            Of course that could just be an extension on the compiler I am using, however the C++ specification makes no mention of a limit of the number of times you may de-reference a pointer, and indeed why should it, its an easy enough operation to do.

            Comment

            • visualbasic1111
              New Member
              • Nov 2007
              • 27

              #7
              Thank You Sir I have got sufficent answer for my question. Thanks

              Comment

              Working...