Showing posts with label sizeof() function. Show all posts
Showing posts with label sizeof() function. Show all posts

Program to determine the number of elements (or size) in a tree.

Solution:

int tree_size(struct node* node)  {
  if (node==NULL)
  {
    return(0);
  }
  else
  {
    return(tree_size(node->left) + tree_size(node->right) + 1);
  }  }

The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?

The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer. sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a block allocated by malloc( ).