Thread: Cases of empty array

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    585

    Cases of empty array

    I'm confusing about something which I'm struggling with.
    sometimes we put this condition sometimes on arrays: (assume we have pointer to array as arr)
    Code:
    if (arr == NULL || arr.length()==0 ) // Base case
     return -1
    so what I'm not understanding is, if arr ==NULL is not saying the size of array 0? I'm totally confused with those two cases , can you please attach example to those two cases? thanks alot alot .


    And in Nodes/binary tree we just say in Base case about nullity
    Code:
    if (root==NULL)
    return 0;
    we are not taking the case of size of the tree is zero .. so why?




    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,213
    Post code that compiles, not paraphrased gibberish.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    585
    Quote Originally Posted by Salem View Post
    Post code that compiles, not paraphrased gibberish.
    I'm not having specific code, it's something in general

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    625
    An array can't be "empty", meaning it can't have zero elements. (With the exception of the last array in a struct, which I'm going to assume that's not the case here.)

    A pointer can be null, which means it doesn't point to anything.

    In the case of a tree, it depends on exactly how it's implemented, but it's possible that if the root pointer is null, then the tree is empty. That's a very different thing from an array having a length of zero.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,595
    @RyanC: I will no longer reply to any of your posts!!!!!!!!
    If you think PM me again is a good idea; I will likely ask that you be banned!!!!

    I hope you can stop being a troll or a help vampire; but, you seem to have learned little in 4 years.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,844
    As an eternal newbie, you aren't allowed to do manual memory management anyway. Stick to std::array when you need a fixed size array and std::vector when you need a dynamically sized array.

    Case closed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Empty Array
    By edmund085 in forum C Programming
    Replies: 5
    Last Post: 09-03-2011, 04:50 PM
  2. How to tell if an array is empty in a structure?
    By lurikeen in forum C Programming
    Replies: 4
    Last Post: 04-02-2009, 09:55 PM
  3. Empty Array?
    By Terran in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 12:47 AM
  4. Empty an Array
    By Charlin in forum C++ Programming
    Replies: 11
    Last Post: 01-12-2008, 02:48 PM
  5. Empty Array
    By devilsknight in forum C Programming
    Replies: 16
    Last Post: 07-18-2006, 09:52 PM

Tags for this Thread