Thread: Is there a way to find the number of elements in an array?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    Is there a way to find the number of elements in an array?

    Here's a simple question that I don't know the answer to. I hope someone knows a trick or two.

    Say you have two arrays and you want to make a single function to print the out the elements. One is size 2 and one is size 3 let's say. Is there a way to read the number of elements in an array so that you don't print out 3 elements if you're printing the array with 2 elements?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could always do something odd like use the first element of the array to hold the total number of elements in the array.
    Code:
    char array[100] = {100};
    Why you'd want to do this is beyond me, but you could.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    would NELEM(x) also work for arrays of pointers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM