CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90

    How many elements in an array of pointers?

    I would think there is a library function to find the number of elements in an array of pointers- such as:
    char xx[] = {"abc", "aaoo", "bb", ......."x"}
    but at the moment am stumped.
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: How many elements in an array of pointers?

    Originally posted by Sirjorj
    I would think there is a library function to find the number of elements in an array of pointers- such as:
    If the array declaration is visible to your code, you can use the sizeof operator, dividing the size of the array by the size of the element type, e.g.
    Code:
    char* xx[] = {"abc", "aaoo", "bb", ......."x"};
    int elementCount = sizeof(xx) / sizeof(char*);
    (Note that the declaration as you posted it with char xx[] is incorrect, as you initialize the array with char pointers, not chars).

    However, if you just have a pointer to the first element (for example, when you pass the array to a function as a poniter or reference) you're out of luck - plain C arrays don't carry information about their size, so there can't be a library function which finds it out.

    In C++, you would rather use a std::vector, which can be queried about the current number of elements.

  3. #3
    Join Date
    Sep 1999
    Posts
    46
    Hi i am simpleman

    i think the above solution is the perfect.

    if it is not char Pointer type array (which means that the element's size is not fixed..), what about using m_nArrayCount variable..

    for examplem, you can declare one integer variable for the element count of the array and increase the counter when you add an element into the array, and decrease the counter when you delete an element from the array.. ^^

  4. #4
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    Hi i am simpleman

    i think the above solution is the perfect.

    if it is not char Pointer type array (which means that the element's size is not fixed..), what about using m_nArrayCount variable..

    for examplem, you can declare one integer variable for the element count of the array and increase the counter when you add an element into the array, and decrease the counter when you delete an element from the array.. ^^
    What's the point of using an OO language if you refuse to reuse the code. Adding a m_nCount variable and managing it would duplicate some ( as in very litle) of the std::vector.

    However if you don't want to use vector ( some strange vow against using STL) you could use an ending mark in the array, such as:

    Code:
    char* xx[] = {"abc", "aaoo", "bb", ......."x", NULL};
    int elementCount = 0;
    for( char** pIt = xx; *pIt != NULL; ++pIt)
    {
        ++elementCount;
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured