Thread: still confused about pointer and binary search tree...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    still confused about pointer and binary search tree...

    so, what is the relationship between a pointer and a tree?
    is it true that every element in a tree must be a pointer?
    and what' is the point of using a pointer...? i really get confused...
    and i need some help with udnerstanding pointer too...

    what is the difference between the following..?
    int *a = 123;

    and


    int *a=(malloc(sizeof(int));
    *a = 123;

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by winggx View Post
    what is the difference between the following..?
    int *a = 123;

    and


    int *a=(malloc(sizeof(int));
    *a = 123;
    The difference is that int * a = 123; is trying to assign the address "123" to the pointer to int a. And pretty much invalid.

    The second statement is allocating the size of an int and assigning that memory to the pointer to int a, then assigning the memory pointed to by a (referenced by *a) the number 123.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by winggx View Post
    so, what is the relationship between a pointer and a tree?

    what is the difference between the following..?
    int *a = 123;

    and
    This is an error - missing a )
    int *a=(malloc(sizeof(int));
    *a = 123;
    Pointers make up the "branches" of a tree. They give it an organized structure. If you want to get to the "fruit", you have to go "monkey style" along the branches.

    What is the difference when you print out their values? Don't be restrained from testing these things out for yourself - that's an excellent way to learn them, so you remember.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    ic...thx for the help...but i got another question...
    what does it mean when there are two "*" when declaring a pointer?

    eg
    Code:
    int main() { 
       int j = 2666, k = 136; 
       int *pti = &j; 
       int **ptpti = &pti; 
       *pti = 241; 
       *ptpti = &k;
       j = **ptpti; 
       **ptpti = 251; 
       pti = malloc(sizeof(int)); 
       *pti = j; 
       k = **ptpti;  
       *pti = 123; 
       j = **ptpti; 
       *ptpti = &j; 
       printf(%d %d\n, j, k);
    }

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    ** means pointer to pointer.
    Generally we will have pointer to integers, pointer to characters.
    Similar to these things ** is a pointer to another pointer.

    Sample pointer to pointer declaration will be like,
    Code:
    int **pp; //pp is a pointer to pointer which can points to an integer
    int *p;//this is a pointer to integer
    int i=20;//normal integer
    p = &i; 
    pp = &p; // pp(pointer to pointer) points to p(pointer to integer) which points to i(integer)
    Last edited by thillai_selvan; 02-28-2010 at 10:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 01:17 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM