Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: about pointers

  1. #1
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post

    about pointers

    I know
    char a; =Variable
    char* a; =Pointer
    but what is
    char** a;
    Last edited by adarshakb; 08-03-2009 at 09:51 AM.
    Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.

    Albert Einstein
    -----------------------------------------------------
    My Blog songs

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Simple! Pointer to a pointer type variable.
    Code:
    char a, *b, **c;
    a='x';
    b=&a;
    c=&b;
    
    printf("%c %c %c", a, *b, **c);
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    Quote Originally Posted by abduraooft View Post
    Simple! Pointer to a pointer type variable.
    Why would one want to use that.. not used til now tats why... Like an array or pointers then i guess
    thanks for clarification
    Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.

    Albert Einstein
    -----------------------------------------------------
    My Blog songs

  • #4
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    well it can be quite useful, an easy example would be this function that changes the size of an array:

    Code:
    void changeArray(int** someArray, int arraySize)
    {
    	delete[] (*someArray);
    	(*someArray) = new int[arraySize];
    
    	for (int i = 0; i < arraySize; i++)
    	{
    		(*someArray)[i] = i;
    	}
    }
    You can't rely on the fact that when you "re-create" the array, that it will be in the same place before (though it might if you don't create anything else on the free store after deleting and re-allocating it). I wouldn't recommend the code I posted though. You could use a C++ reference and you should use vectors for something like this instead because if the array is allocated at a different position than before, all other pointers pointing to that array are left pointing to a possibly deleted block of memory.

    Basically if you want to change the adress that a pointer is pointing to using a function, you'll need a pointer to this pointer in order to change it, otherwise you would only be copying the adress its pointing to. But if you're programming in C++ you could just have a reference to a pointer as a parameter like so: int*&.
    Last edited by BrickInTheWall; 08-03-2009 at 10:16 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •