Thread: Counting number of strings in an array

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Counting number of strings in an array

    Hey there!

    I have an array of strings where I declare all strings like this:
    Code:
    string[2] = "\xFB\x9D";
    to represent 2 chars in one variable. All tend to be only 2 bytes long.

    Code:
    char a[2] = "\xFB\x01";
    char b[2] = "\xFF\x01";
    char c[2] = "\xD1\xF5";
    ...
    
    char *pitems[] = {a, c, f, z};
    What would be the safest&easiest way to count the number of strings in this array? Because they are not \0-terminated I can't use strlen().

    Thanks in advance,
    Hawk

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you do not pass the array to a function, but are in the block where it is defined:
    Code:
    sizeof(pitems)/sizeof(pitems[0])
    If you do pass the array to a function, you're just out of luck.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well actually this
    Code:
    char *pitems[] = {a, c, f, z};
    is declaring an array of char pointers, so you can easily add a NULL pointer at the end and use that to know hoiw many of them there are in any function this is passed to. E.g:
    Code:
    char *pitems[] = {a, c, f, z, NULL};
    foo(pItems);
    
    //...
    
    void foo(char** pItem)
    {
        while (pItem != NULL)
        {
            bar(*pItem);
        }
    }
    Last edited by laserlight; 10-24-2009 at 03:35 AM. Reason: Closed code tag.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thank you for the input. I (finally) added both methods to my program to find them if I need them in another program also. Yet I don't know why sizeof(pitems)/sizeof(pitems[0]) wouldn't work if I passed the array to another function.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since arrays sizes degrade to pointers when they're passed to a function, the sizeof(arrayName) becomes just the size of the pointer to that data type (int, char, whatever).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. How do u write up an array of strings?
    By hot_ice in forum C Programming
    Replies: 1
    Last Post: 11-19-2001, 08:41 PM