-1

Is there any better way of getting the right length of array containing digits?

I have an array of digits: 0, 0, 1 and I try to get length of it. It obviously breaks and returns 0. I am new to C but I tried to make custom strlen function:

int custom_strlen(char *str) {
    for(int i = 1; ;i++) {
        if (str[i] == 0) {
            return i;
        }
    }
    return -47;
}

but it is not that efficient and in some cases returns unexpected values as well. The expected out put would be 3 in this case.

Is there any function to use?

Epsilon47
  • 768
  • 1
  • 13
  • 28
  • 1
    There is no way to do this if you don't use a sentinel (i.e.: a non digit value), (you don't know when to stop) – David Ranieri Oct 23 '18 at 18:46
  • If it's not a NUL terminated string you can't run `strlen` on it. How are you terminating this "array of numbers"? – tadman Oct 23 '18 at 18:46
  • Nope, if the array may contain zeros at any location, then it's not a C string, and none of the string functions can be used. – user3386109 Oct 23 '18 at 18:46
  • You should start with `i=0` in case of an empty string. – Osiris Oct 23 '18 at 18:47
  • 1
    Please create an MCVE ([MCVE]). It will address all sorts of "whaddya mean by …" questions if we can see see enough code. Why do you start looking at array index `1` instead of `0`? Arrays in C index from 0, not 1. Your code would fail on an empty string (or, rather, return an unpredictable length due to undefined behaviour accessing data beyond the end of the string). – Jonathan Leffler Oct 23 '18 at 18:51
  • What's with `return -47;` ? Do you realize that line of code will never be executed? – Carey Gregory Oct 23 '18 at 18:52
  • @CareyGregory That's obviously a "if this this returns -47, something is VERY wrong" – klutt Oct 23 '18 at 18:53
  • @Broman Yeah, I get that, but as I said, it will never be executed. It's utterly pointless. – Carey Gregory Oct 23 '18 at 18:54
  • I am sorry, the code I wrote was not a good attempt. I will update the question. – Epsilon47 Oct 23 '18 at 18:54
  • 2
    What exactly are the contents of this "array of digits"? The usual way to represent binary numbers is to use `'0'` and `'1`', not `0` and `1`. If you do it that way, you can use the predefined `strlen` function. Both your function and the standard `strlen` have to assume that there's a terminating `'\0'` somewhere in the array. – Keith Thompson Oct 23 '18 at 19:01
  • @KeithThompson thank you. I did know that - helped a lot. Again sorry for unclear question I am still learning the references and basics – Epsilon47 Oct 23 '18 at 19:17
  • If you have an array of `'0'` and `'1'` characters (an array that's not necessarily a null-terminated string), and you want to find the first character that's not a `'0'` or a `'1'`, I suppose you could use [`strspn`](https://linux.die.net/man/3/strspn). – Steve Summit Oct 23 '18 at 19:29
  • "I have an array of digits: 0, 0, 1' --> Yet the post does not show the code that support that. Post the code the has your array of digits. IOWs, how is `custom_strlen()` called. – chux - Reinstate Monica Oct 23 '18 at 20:10

2 Answers2

3

An array of integers is not a string. C arrays do not contain length information inherently. The way strlen works is that C strings are null terminated, meaning the last character is NUL (null character), which is 0. Otherwise, there is just no way to know how long an array is.

I think you may be wanting to do an array of '0','0','1'. Can you post the array you are using?

ChilliDoughnuts
  • 367
  • 2
  • 13
0

As mentioned, C strings are null terminated.

The only choices are

  • Using a string terminated with some special character that you watch for (like a null) or
  • Keeping track of how long the string is when you create it.

FWIW, if it's not null terminated, it's not actually a string in C, it's just memory that contains chars that you happen to be interpreting as a string.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32