array length question

This is a discussion on array length question within the C Programming forums, part of the General Programming Boards category; simple array question from a c beginner. basically i have an integer array as an argument to a function. my ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    array length question

    simple array question from a c beginner.
    basically i have an integer array as an argument to a function. my problem is getting its length. sample code of what i have so far is:

    int arrLen (int a[]) {
    return (sizeof a / sizeof a[0]);
    }

    but this returns 1, which i'm guess is 4 bytes divided by 4 bytes. any ideas?
    any help would be appreciated!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Either pass the size as a parameter (using the same construct in which the array name is in scope), or use some type of delimiter such as -1 to indicate the end of the array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    other than passing the size as an argument of using a delimeter, are there any other alternatives??

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by cs_newbie
    other than passing the size as an argument of using a delimeter, are there any other alternatives??
    Only if you count variations on the above, such as making the first element of the array the length.

    When you use int a[] in the parameter list of a function declaration it's equivalent to int *a; the only information the function has about a when called is a pointer to the first element.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    You could make a user-defined type for arrays or something, but what Dave's already suggested is practical enough I think.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could use something like this to do what your function originally intended:
    Code:
    #define arraysize(a) (sizeof(a)/sizeof(*a))
    Or you could just put this in your code directly:
    Code:
    sizeof(array)/sizeof(*array)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That won't work. The OP is trying to get the size of a passed array. There is no way to do that. Your code won't work.


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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I realize that, but if all the OP was trying to do was to create a function that returns the number of elements in an array, my suggestions would work. (Getting rid of the function and using a preprocessor directive instead.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not necessarily. It only works if it's in scope. If it's in scope, you already know how big it is.


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

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I know that. I thought that the OP was trying to do something like this:
    Code:
    int array[whatever], x;
    
    for(x = 0; x < length(array); x ++) {}
    In which case two sizeof()s or a macro that does the same would do what [s]he intended.

    You can't win an argument with Quzah.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    You can't win an argument with Quzah.
    You can. Just not if you're wrong.


    Quzah.
    Last edited by quzah; 10-08-2006 at 01:40 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could try

    Code:
    #define wrapper_foo(x) foo(x, sizeof(x)/sizeof(int))
    
    void foo(int arr[], int size)
    {}
    
    int main()
    {
        int arr[80];
        wrapper_foo(arr);
    }
    Another creative use of the preprocessor I guess. Just remember to only call wrapper_foo() on arrays in the current scope!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by cs_newbie
    simple array question from a c beginner.
    basically i have an integer array as an argument to a function. my problem is getting its length. sample code of what i have so far is:

    int arrLen (int a[]) {
    return (sizeof a / sizeof a[0]);
    }

    but this returns 1, which i'm guess is 4 bytes divided by 4 bytes. any ideas?
    any help would be appreciated!
    yeah even though you type

    int arrlen(int a[])

    as parameter, it is exactly the same as if you typed

    int arrlen(int *a)

    array parameters always decay to a pointer when passing as arguments and then getting the size anymore is not possible. The pointer could be a pointer returned from malloc or point to the middle of another array.
    So what you often see is the extra size parameter to functions that need array size.

    void func(int arr[], size_t size) {}

    Then if you declare int arr[10]; you can pass (sizeof arr / sizeof *arr) to it
    The compiler knows that getting the size of arr[10] is possible because it is illegal to do arr++ on a statically allocated array.

    Another thing you see sometimes is the sentinel at the end of an array. Choose a special value as the last element of the array and then you dont need the extra size parameter anymore. The disadvantage can be that you have to loop through the whole array to get the length.

    the C standard library uses it for strings. strlen(s) returns the length of a string, it relies on the array to be zero terminated though.

    Or the length can be stored in memory before where the pointer points to.
    for instance

    Code:
     int *arr = malloc(10 * sizeof(int) + 1); // +1 for the length variable.
    
      *arr = 10;
      arr++;
    
      foo(arr);
    
      free(arr-1); // free the pointer that was allocated, so -1
    
      void f(int * arr)
      {
        int length = arr[-1];
      }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's just nasty. Why would you index at -1? That's just begging for misuse. You should call your function "pleaseCrashMyApp".


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

  15. #15
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    @quzah... They ARE watching... really familiar.. from which movie did you take that one may i ask?
    It is not who I am inside but what I do that defines me.

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

Similar Threads

  1. A quick array question
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-30-2006, 03:18 AM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  3. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:04 AM
  4. 2-dem array
    By alika in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 10:59 AM
  5. reading in array and returning length
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:25 PM

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