-1

I need to find the length of an array, how would I do this without using the sizeof function.

eg if

Array 1 = [0 1 2 3 4 5 6]

the size of this array would be 7.

alk
  • 69,737
  • 10
  • 105
  • 255
Peter Sand
  • 25
  • 8

2 Answers2

5

If you can't use sizeof (tell us why, please), you can use a loop and a sentinel (-1 or some number that can not be used in the array):

int arr[] = {0, 1, 2, 3, 4, 5, 6, -1};
int count = 0;

while (arr[count] != -1) count++;
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    I would have used the condition `!= -1`. – alk Oct 05 '15 at 10:39
  • It's just that I had to write a small piece of code(function) without using builtin functions other than the ones from stdio.h – Peter Sand Oct 05 '15 at 10:40
  • 2
    @PeterSand `sizeof` is an operator, not a function. In case that makes a difference to your problem. – kaylum Oct 05 '15 at 10:42
  • `int a[10]; int *fun(int *a) {return a; } printf("%d",sizeof(fun(a)));` This would print `4` not `40`. Maybe OP has a case like this – Nishant Oct 05 '15 at 10:42
  • @ameyCU, are you sure?, `&Array_1[1] - Array_1` is always `1` for me – David Ranieri Oct 05 '15 at 10:56
  • 1
    @AlterMann You missed bracket . Its `(&Array_1)[1]` not `&Array_1[1]` .That will make difference. – ameyCU Oct 05 '15 at 10:57
  • @ameyCU, Oooh, I get you, but then you need a 2D array, isn't it? – David Ranieri Oct 05 '15 at 10:59
  • @AlterMann Its not address of second item . That first expression will be evaluated to this `*(&Array_1 +1)` . No , we don't need 2-d array. – ameyCU Oct 05 '15 at 11:02
  • 2
    @ameyCU ...which contradicts [what you've said](http://stackoverflow.com/a/32537682/3049655) – Spikatrix Oct 05 '15 at 11:03
  • @CoolGuy But it wasn't done that if it evaluated or not . I think it was not completely discussed. Thanks for pointing it out . My bad i withdraw my previous comment. – ameyCU Oct 05 '15 at 11:04
  • @ameyCU, yes, as pointed out by CoolGuy and yourself, if the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated. – David Ranieri Oct 05 '15 at 11:05
  • 1
    @AlterMann Ohh my mistake , that was not a good thing to point to HIM, I would withdraw my previous comment. – ameyCU Oct 05 '15 at 11:07
2

Many high-level programming language save the length of an array once it is created.

/* e.g. Java */
int[] foo = new int[10];
assert(foo.length == 10);

But the length of an array is not saved in C! This is useful as you can decide how you want to save the length with respect to optimization. You basically have three possibilities to get/save the length:

mark the end of the array with a certain value (i.e. \0 is used for strings)

char foo[] = "bar";
/* foo has length 4(sic!) as '\0' is automatically added to the end*/
int i = 0;
while(foo[i] != '\0'){
    printf("%c",foo[i]);
    i++;
}

save the length of the array in a variable

int foo[] = {1,2,3,4};
int length = 4;    
for(int i = 0; i < length;i++){
    printf("%i, ",foo[i]);
}

use sizeof (warning: sizeof is (mostly) computed at compile time and its use is restricted. you can only use sizeof within the function where the array has been created. when you pass an array to a function you only pass the pointer to the first element. therefore you can loop through this array as you know what offset must be used(type of its elements), but you do not know how big it is unless you also passed the length or added a sentinel value)

/* ok */
int foo[] = {1,2,3,4};
for(int i = 0; i < sizeof(foo)/sizeof(int);i++){
    printf("%i, ",foo[i]);
}

/* not ok */
void foo(int bar[]);
void foo(int bar[]){
    for(int i = 0; i < sizeof(bar)/sizeof(int);i++){
        printf("%i, ",bar[i]);
    }
}
int main()
{
    int arr[] = {1,2,3,4};
    foo(arr);
    return 0;
}
jboockmann
  • 1,011
  • 2
  • 11
  • 27