How to determine length of pointer array

This is a discussion on How to determine length of pointer array within the C Programming forums, part of the General Programming Boards category; Hello, Just a quick question, already googled etc.. If I have a pointer array lets say: Code: char * fruit[] ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    16

    How to determine length of pointer array

    Hello,

    Just a quick question, already googled etc..

    If I have a pointer array lets say:

    Code:
    char * fruit[] = {"apple", "pear", "orange", "grape", "banana"};
    Its just the program I am witting uses a pointer array which can change in size and I need to know how many variables are inside the array after it has changed size. For example there is 5 fruits in the above array.

    I don't want to declare the array as a set length.

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Code:
    sizeof fruit/sizeof fruit[0];

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you mentioned changing size. if that's the case then you have to keep the size in a separate variable. the sizeof trick won't work on dynamic arrays and it only gives you the capacity on non-dynamic arrays.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    Code:
    sizeof fruit/sizeof fruit[0];
    that won't work because the fruits are not all the same size! [edit: I'm wrong about that, but this would work:] You could add an extra NULL pointer at the end:
    Code:
    char * fruit[] = {"apple", "pear", "orange", "grape", "banana", NULL};
    And as long as you keep one there, you could use a function that will iterate thru one element at a time until fruit[X]==NULL.

    But Meldreth's idea is more efficient; you just need an extra variable and to make sure it is always incremented and decremented appropriately.
    Last edited by MK27; 02-16-2009 at 03:39 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    that won't work because the fruits are not all the same size!
    they're all the size of a char pointer. sizeof doesn't reach into the pointer and see how long the string it points to is. it works as-is as long as it's ok to always get 5 as a result. i don't think that's what Dr.Zoidburg wants.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    >> that won't work because the fruits are not all the same size!
    Yes they are, and yes it will.

    The definition of fruits makes it so. Each fruit is the size of a pointer and the size of the array is the sum of all the pointers. Dividing through gives you the number of elements as long as the array was defined in the same scope.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    Thanks for the lightening fast replies. I'll give them a go now.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    Yes they are, and yes it will.
    You and Meldreth are right, sorry. Perhaps if someone had written:
    Code:
    sizeof fruit/sizeof *char
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by MK27 View Post
    Perhaps if someone had written:
    Code:
    sizeof fruit/sizeof *char
    so what do you think is the type of fruit[0]
    the compiler auto adds a NULL to the end of (const char* type) arrays so no need to do so explicitly.
    Last edited by itCbitC; 02-16-2009 at 05:42 PM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    so what do you think is the type of fruit[0]
    the compiler auto adds a NULL to the end of (const char* type) arrays so no need to do so explicitly.
    Do you mean to say that this:
    Code:
    char * fruit[] = {"apple", "pear", "orange", "grape", "banana"};
    actually looks this way:
    Code:
    char * fruit[] = {"apple", "pear", "orange", "grape", "banana", NULL};
    I would say not. Try this:
    Code:
    char * fruit[] = {"apple", "pear", "orange", "grape", "banana"};
    char * veg[] = { "tomato", "cabbage", "celery" };
    char *dummy[] = { NULL };
    int main()
    {
        char **ptr = fruit;
        while(*ptr)
        {
           printf("%s is a fruit\n", *ptr);
           ptr++;
        }
    }
    You'd sure to see tomato, cabbage and celery printed as fruits.

    A character array in double quotes, where there is no fixed size, will have a NUL-character at the end. But any other arrays will have nothing more in them than what you put there - unless you specify a size, and it is an initalized array, the rest of the array is filled with zeros.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Ah! yes you're absolutely right and I'm totally wrong. My bad I got the auto NULL terminated string confused with an array of pointers to char.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Ah! yes you're absolutely right and I'm totally wrong. My bad I got the auto NULL terminated string confused with an array of pointers to char.
    Glad to have cleared that one out...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058

    Thumbs up

    way to go matsp...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 12:07 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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