count elements in an array

This is a discussion on count elements in an array within the C Programming forums, part of the General Programming Boards category; Hi, all! do you know if there is any predefined function to count all the element in an array? or ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Thumbs up count elements in an array

    Hi, all!

    do you know if there is any predefined function to count all the element in an array? or we have to write such functions ourselves?

    for instance: if i have an array like this below:

    Code:
    int intMyArray[]={1,2,3,4,6,7};
    how can we find out if there are 6 elements in the array intMyArray[] ??

    usually, Iwrite

    Code:
    count  = sizeof (intMyArray)/sizeof(int); // this work, it gives me correct value
    however, when I pass the array to a function like,

    Code:
    #include <stdio.h>
    
    int countArrayElement(int arr[]);
    
    int main()
    {
    int intMyArray[]={1,2,3,4,6,7};
    countArrayElement(intMyArray);
    return 0;
    }
    
    int countArrayElement(int arr[])
    {
    int count = 0;
    count  = sizeof (arr)/sizeof(int);  // wont work at all, arr is just the first element
    return count;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, you have to keep track of the size yourself (when you pass by pointer).

    Something like:
    Code:
    int countArrayElement(int arr[], size_t elements)
    FYI, I like using,
    Code:
    size = sizeof(array) / sizeof(array[0]);
    So if you change the type of array it's not going to mess up.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Smile size_t????

    Quote Originally Posted by zacs7 View Post
    No, you have to keep track of the size yourself (when you pass by pointer).

    Something like:
    Code:
    int countArrayElement(int arr[], size_t elements)
    FYI, I like using,
    Code:
    size = sizeof(array) / sizeof(array[0]);
    So if you change the type of array it's not going to mess up.
    Then, I have one question, why do we have to use size_t????, why don't we use int??
    i just know that size_t can be the same as unsigned integer or unsigned long. I hope you can clarify this, since I have been so wondering since the time I start to learn C!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because the elements of an array are unsigned (0 to whatever), they can't be negative. You don't have to use size_t, but it's good to. Read http://www.embedded.com/columns/prog...0900195?pgno=2

    size_t can be implemented as signed, but then it'd be signed everywhere else (ie in malloc(), sizeof() etc).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    size_t can be implemented as signed
    size_t has to be an unsigned integer type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I knew that, except the link I posted states otherwise (pre GCC 2.4), read it
    Last edited by zacs7; 11-25-2007 at 05:42 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    That'd make those compilers non-standard compliant?
    Yes, at least according to my copy of C99. I wonder why those compilers have it as signed in the first place, hmm...
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't think they were complying in the first place. I edited my post, it seems it was a GCC 2.4 and before problem.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    A signed size_t variable could be a nightmare when come upon unexpected. lol..... ugly.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's my observation / understanding that having typedefs such as size_t allows programmers to insulate themselves from compilers, operating systems and specific hardware platforms.

    For instance, in old DOS program, an integer was 2 bytes. Today it's 4 bytes. (I don't know if this is the actual case, but) size_t could have been defined as 4 bytes for an old DOS program and today, now its still 4 bytes, even though the operating systems and hardware have both advanced. Therefore, having the typedef insulated you, the programmer, from having to change your program to keep up with changes to the operating system and hardware.

    Also, it's now common to typedef a char data type, to support wide characters (for Unicode support), again insulating your program (somewhat perhaps) from this type of change as well.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left Shift elements of a 2D array
    By w2look in forum C Programming
    Replies: 8
    Last Post: 01-22-2009, 11:13 PM
  2. Counting array elements
    By katipo in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 06:10 PM
  3. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 01:41 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM

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