count strings in an array?

This is a discussion on count strings in an array? within the C Programming forums, part of the General Programming Boards category; Hi, I've a two-dimensional character array, initialized as follows: Code: char *valid_help_str[] = {{"help"},{"help add queue"},{"help add queue default"},{"help add ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    count strings in an array?

    Hi,

    I've a two-dimensional character array, initialized as follows:

    Code:
    char *valid_help_str[] = {{"help"},{"help add queue"},{"help add queue default"},{"help add queue basic"},{"help add queue medium"},{"help add queue advanced"}};
    How to count number of strings present in this array?

    Thank you

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    If the array is in scope, you can do this

    Code:
    size_t numElements = sizeof(valid_help_str) / sizeof(valid_help_str[0]);
    This does NOT work if you pass the array to a function, and try to do it inside the function.

    You can add some newlines to your array you know, so we don't have to scroll horizontally as well.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    You can find the size of the array and divide it by the size of a char pointer:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char *valid_help_str[] = {
            "help",
            "help add queue",
            "help add queue default",
            "help add queue basic",
            "help add queue medium",
            "help add queue advanced"
        };
    
        printf( "Number of elements = %lu\n", sizeof( valid_help_str ) / sizeof( char * ) );
    
        return 0;
    }
    The output from this program is "Number of elements = 6" which is expected.

    Kevin

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sangamesh View Post
    Hi,

    I've a two-dimensional character array, initialized as follows:

    Code:
    char *valid_help_str[] = {{"help"},{"help add queue"},{"help add queue default"},{"help add queue basic"},{"help add queue medium"},{"help add queue advanced"}};
    How to count number of strings present in this array?

    Thank you
    Since you are simply assigning string constants, there should be no reason to count anything... you should already know how many strings *you programmed* into the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of getchar / count
    By Marth_01 in forum C Programming
    Replies: 20
    Last Post: 11-19-2008, 06:36 PM
  2. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 07:05 AM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. count array value
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 10-05-2004, 10:02 AM
  5. Using an Array to store the count.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 05:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21