Thread: how to determine the length of array

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    You already have an expression that does that, in the main function. You just need to recognise when it can, and cannot, be used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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?

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, 02:25 PM
  2. Array length error using strlen()
    By wirefree101 in forum C Programming
    Replies: 3
    Last Post: 11-11-2009, 10: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, 01:01 AM