1

I have tried (sizeof(array)/sizeof(array[0])). Didn't work. I wrote a simple function:

int length(int array[]){
    int i=0;
    while(array[i]) i++;
    return i;
}

Worked one minute, didn't work the next.

Someone please help! I'm using Xcode as an IDE

benwaffle
  • 322
  • 1
  • 3
  • 12
  • 6
    This should help you: http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c. `sizeof` doesn't work because it's being passed to a function, and the other doesn't because it's meant for null-terminated arrays (usually strings). – chris Nov 15 '12 at 05:24
  • @chris Despite the URL, that link is to a question about C++, not C. – Jim Balter Nov 15 '12 at 05:46
  • @JimBalter, True, but with a basic understanding of C++ syntax, nearly all of it is just as valid for C with the small things converted, such as `new[]` -> `malloc`. If I could find a C version, I'd gladly use it. – chris Nov 15 '12 at 05:51
  • @chris I think you're failing to appreciate just how much of a novice the OP is ... I suspect that it will overwhelm. – Jim Balter Nov 15 '12 at 05:59
  • @JimBalter, I see your point, though answers typically provide a good explanation as well. The link does help anyone who is beyond novice that comes here with the same misunderstanding, though. – chris Nov 15 '12 at 06:02
  • `int array[]` when written as a function parameter, defines an array pointer. When the same is written inside a function (at local scope), it defines an array. In case you take sizeof(an array pointer), you get 4, assuming your system uses 32-bit addresses. – Lundin Nov 15 '12 at 07:52

5 Answers5

3

The length of an array is not part of the array in C, so when passing an array as a parameter to a function you should pass its length as a parameter too. Here's an example:

#define ARRLEN(a) (sizeof(a)/sizeof (a)[0]) /* a must be an array, not a pointer */

void printarray(int* a, int alen)
{
    int i;
    for (i = 0; i < alen; i++)
        printf("%d\n", a[i]);
}

main()
{
    int a[] = { 3, 4, 5 };
    printarray(a, ARRLEN(a));
    return 0;
}

However, if your array is defined in such a way as to always end with a sentinel that isn't normal data, then you can traverse the elements until you encounter the sentinel. e.g.,

void printstrings(char** a)
{
    int i;
    for (i = 0; a[i]; i++)
        printf("%s\n", a[i]);
}

main()
{
    char* a[] = { "This", "should", "work.", NULL };
    printstrings(a);
    return 0;
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • i'm trying to get the length so i can't pass it in. besides, i'm calling this function from within another. – benwaffle Nov 15 '12 at 05:48
  • 2
    You can't "get" the length, you have to already know it ... the function that is calling this one knows it, or its caller does, or its ... because somewhere along the line you defined or allocated the array, and the length is known at that point. Please believe me, I've been writing C since 1977 and my answer is sound. – Jim Balter Nov 15 '12 at 06:01
  • i created the array like this: int array[] = {}; – benwaffle Nov 15 '12 at 06:05
  • @user1184160 That's not legal C, as it's an attempt to create a 0-length array. But the point is that the length, whatever it is, is known at the point of declaration, so you could use the sizeof calculation to get the length and then pass that as a parameter. – Jim Balter Nov 15 '12 at 06:09
  • @user1184160 Compiles with what compiler? Some compilers accept invalid C code. And what do you mean by "works"? `array` has no elements. I've been trying to help you here, but if you know what does and doesn't work then I guess you don't need me. – Jim Balter Nov 15 '12 at 06:27
1

Passing an array into a function is the same as passing a pointer to the array. So sizeof(array)/sizeof(array[0]) does not work.

You can define a global macro:

#define A_LEN(a) (sizeof(a)/sizeof(a[0]))
Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
0

There are several methods to get the length of array inside a function (can be in the same file or in different source file)

  1. pass the array length as a separate parameter.
  2. make array length as global extern so that function can directly access global data
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • The 3rd one is little lengthy but no need to pass the size and also no need to make global or create macro – Omkant Nov 15 '12 at 05:56
  • Also compile with `-std=c99` – Omkant Nov 15 '12 at 05:57
  • The `obj` in main is different from the `obj` in func, which is uninitialized. You can't magically get the length into func; you need (1) or (2), or a struct that contains both the length and a pointer to the array (and a struct of that sort for every type). – Jim Balter Nov 15 '12 at 06:04
  • yeah : @JimBalter: i havn't thought about it. in my case it just worked so i posted sorry .. going to edit it – Omkant Nov 15 '12 at 06:12
  • point is `n` (i.e. sizeof array) is not the global now it's inside object so it's safe no hacks – Omkant Nov 15 '12 at 06:30
  • I don't see the point of passing a pointer to a structure containing the length rather than just passing the length ... neither is global. And the idea that values in structs are "safe" from hacks is ... well, it's just wrong. But whatever ... I'm done with this chat. – Jim Balter Nov 15 '12 at 06:33
0

Try this

main()
{
int a[10] ;
printf("%d\n", sizeof(a)/sizeof(int) ) ;
}

output : 10 
Pavunkumar
  • 5,147
  • 14
  • 43
  • 69
  • The OP needs the length in a function other than the one in which the array was declared (the OP stated this in a comment on my answer). – Jim Balter Nov 15 '12 at 06:08
0

See Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? from the comp.lang.c FAQ to understand why the sizeof method doesn't work. You probably should read the entire section on arrays.

Regarding your length function, your "simple" function can work only if your array happens to have the semantic that it is terminated with an element that has the value of 0. It will not work for an arbitrary array. When an array has decayed into a pointer in C, there is no way to recover the size of the array, and you must pass along the array length. (Even functions in the C standard library are not immune; gets does not take an argument that specifies the length of its destination buffer, and consequently it is notoriously unsafe. It is impossible for it to determine the size of the destination buffer, and it therefore cannot prevent buffer overflows.)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204