-4

my professor has assigned a project where we need to make a reverse polish calculator in C (input in postscript syntax). I'm currently working on a method to find the length of the array of values I have scanned in (via .txt file). My current method is

int length(int list[]) {
    int c = 0;
    while(0 == 0) {
        if(list[c] != '\0') {c++;}
        else {break;}
    } 
    return c;
}

and the call for it is

int sizeA = length(list);
printf("\n%d\n", sizeA);

It's currently only outputting the length as 0. Does anyone know why that might be and a fix to this method? Thanks

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    you just rewrote `strlen` for a list of integers... – Jean-François Fabre Nov 22 '16 at 20:58
  • 2
    "I'm currently working on a method to find the length of the array of values I have scanned in" - if you don't already know the length by that point, you probably have a buffer overflow in the code that reads the values. – user2357112 Nov 22 '16 at 20:58
  • List of integers with `list[c] != '\0'` check.. – Stefan Nov 22 '16 at 21:00
  • Since there wouldn't be a `'\0'` in an array of integers, you probably would have to define your own "terminator" to keep track of where the array ends. – abacles Nov 22 '16 at 21:02
  • 1
    Theodore, you don't need that function at all. Instead, when you are reading the numbers, *keep count in a variable*. (I wanted to make sure you understand what the above commenters are talking about.) – Nominal Animal Nov 22 '16 at 21:07
  • Tempted to apply [dup question](http://stackoverflow.com/questions/17590226/c-finding-length-of-array-inside-a-function) but the details are not clear enough. – Weather Vane Nov 22 '16 at 21:07
  • In C, the size of an array is not handled by the language but it is something that the programmer must handle. Usually, one would use one variable for the array and another variable for the size. – Phil1970 Nov 22 '16 at 21:07
  • `'\0'` is more applicable to `char` arrays that are strings. Using a sentinel in an `int` array is not wise, better to pass the length. – Weather Vane Nov 22 '16 at 21:10

1 Answers1

1

The notion of "length" is a sort of tricky one in C (and low-level programming in general). If you have an array, the C compiler knows how large it is and provides an interface to the programmer to get that value in bytes: sizeof. The thing is, arrays are passed via pointers in C and determining the size via pointers is impossible without certain meta-information. Common methods to determine the length of an array are

  • appending an end marker to the array. Determining the length is simply a matter of iterating until the end marker is found and returning the number of iterations. Note that this renders the end marker's value unavailable for use as a value in the array.

  • just passing the size of the array around. Take the write system call as an example. Besides the file handle, it needs a pointer to the data and its length. Why its length as well? Because the pointer doesn't contain information about the length. So, either use a terminator like a null byte or pass the length explicitly. The former idea can be abandoned because the write system call is supposed to be generic; and to yield genericity, a null byte must be expected to be a possible value in the array, so it cannot be used as a terminator for reasons I uttered above.

Which one you actually end up using totally depends on the particular use case.


Apparently you decided to use the terminator-variant. \0 is the null byte, an ASCII character with code value 0x0. It's commonly used for terminating C-strings. strlen uses it to determine a C-string's length, for example.
For int arrays, there is no such predefined terminator, so you need to come up with your own one. If you decide on \0, so be it, but I'd use the literal 0x0 instead because strictly-speaking, \0 is a character literal and that's just unfitting for ints.

To actually implement this, you'd need to append the terminating value to every int array, whose size you want to determine this way. Then write your function to get the length of such an int array just as you do, i.e., by iterating until the terminator is found.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67