how to determine the length of array

This is a discussion on how to determine the length of array within the C Programming forums, part of the General Programming Boards category; Hi, all i am a newbie to cprogramming , could you please point the difference of following code segments of ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    how to determine the length of array

    Hi, all

    i am a newbie to cprogramming , could you please point the difference of following code segments of determing length of array ??


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    void print_length(void * p)
    {
            int len = sizeof(p) / sizeof(p[0]);
            printf("the length of str is %d\n",len);
            return ;
    }
    
    
    
    
    int main()
    {
            char str[11] = "hello world";
    
            int len = sizeof(str) / sizeof(str[0]);
            printf("the length of str is %d\n",len);
    
            print_length(&str);
            return 0;
    }
    linux@localhost:/data/learning/C/variantLen.c> gcc -o length length.c 
    linux@localhost:/data/learning/C/variantLen.c> ./length 
    the length of str is 11
    the length of str is 4

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Hint: when passed as an argument, an array is converted to a pointer to its first element. The sizeof an array is not necessarily the same as the sizeof a pointer to the first element of the same array.

    By the way, sizeof(p[0]) does not make sense: you are effectively dereferencing a pointer to void, and then trying to find sizeof(void).
    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

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Smile

    if so, could you please give me example function which can determine the length of an given array.


    thanks a lot

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You already have an expression that does that, in the main function. You just need to recognise when it can, and cannot, be used.
    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

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    You cannot. As laserlight pointed out, once an array is passed to a function, it's no longer an array (in that function).

    What you can do is mark the end of the array somehow (as C does by using a null character at the end of a string) and search for that. However, your code implies that you want to be able to pass in any array type and find its length. Sorry, but that's not going to work. You could use a macro, though:
    Code:
    #define ARRSIZE(a) (sizeof (a) / sizeof *(a))

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Hmm. i understand it
    thanks to all of you for your quick reply

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Pass along the size of the array to the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    74
    btw, char str[11] = "hello world";
    should be: char str[12] = "hello world";

    don't forget to add space for the '\0' character.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Better yet,
    char str[] = "hello world";
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by noobiept View Post
    btw, char str[11] = "hello world";
    should be: char str[12] = "hello world";

    don't forget to add space for the '\0' character.
    Actually both of those are legal. One is a string, the other isn't.


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

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    74
    Isn't it always a string when you declare with "" ?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by noobiept
    Isn't it always a string when you declare with "" ?
    It is a string literal, yes, but the array initialised with the string literal might not constitute a string.
    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

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    74
    could you elaborate? in what circumstances it doesn't evaluate to a string?

    Whenever I see a initialization with the "" I normally assume its a string.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by noobiept
    in what circumstances it doesn't evaluate to a string?
    There is no null character since the array does not have enough space to hold it.
    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

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    74
    Ok, but isn't that like, a mistake?
    Do people do that sort of thing?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. length of dynamic array
    By budala in forum C Programming
    Replies: 12
    Last Post: 02-12-2011, 01:25 PM
  2. Array length error using strlen()
    By wirefree101 in forum C Programming
    Replies: 3
    Last Post: 11-11-2009, 09:11 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. Making a script with arbitrary array length
    By Bri Rock in forum Linux Programming
    Replies: 3
    Last Post: 07-15-2004, 08:59 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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