Thread: Measuring the length of a string with a for loop

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    5

    Question Measuring the length of a string with a for loop

    Hey!
    So I have a simple program that measures the length of a string using a for loop.

    Code:
    #include <stdio.h>
    
    int main(void) {
        unsigned i;
        char str[16] = "ABBA";
    
        for (i = 0; str[i] != '\0'; ++i);
    
        printf("Len: %u", i);
    
        return 0;
    }
    It works fine, but there's one thing I can't wrap my head around. If I set the initial value of i to 1 inside of the loop the program still outputs 4 in fact it always outputs 4. Even if I set the value of i to something like 2 or 3.

    Here's the thing, shouldn't it output 3 if i starts at str[1]? The loop should start at 'B' and finish at 'A' which is three characters. But that's not the case for some reason. Can anybody explain this to me?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,711
    for (i = 1; str[i] != '\0'; ++i);

    Is no different to this, having gone round the loop once.
    for (i = 0; str[i] != '\0'; ++i);

    Compare with
    Code:
    int main(void) {
        unsigned i, count;
        char str[16] = "ABBA";
     
        for (count = 0, i = 0; str[i] != '\0'; ++i, ++count);
     
        printf("Len: %u", count);
     
        return 0;
    }
    Now you'll see a difference whether you start at i=0 or i=1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339

    Thumbs up

    Code:
    int stringLength(char *string) {
        int i = 0;
        while (string[i] != '\0') i++;
        return i;
    };
    Last edited by Structure; 04-28-2021 at 12:33 PM.
    "without goto we would be wtf'd"

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    18
    I want to point out that setting i to values above 0 does actually do something bad in your case: if your string's length is less than the initial value of i, you will be accessing memory you shouldn't be accessing, potentially causing a segfault or worse side effect, including returning the length of a different string!
    Last edited by erikkonstas; 04-28-2021 at 03:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read string length when string contains the null character
    By DecoratorFawn82 in forum C Programming
    Replies: 4
    Last Post: 01-06-2018, 09:07 AM
  2. Measuring the length
    By kiros88 in forum C Programming
    Replies: 7
    Last Post: 09-11-2009, 04:32 PM
  3. help with string length loop.
    By k4k45hi in forum C Programming
    Replies: 10
    Last Post: 09-19-2007, 04:30 AM
  4. Measuring string length
    By Zaarin in forum C++ Programming
    Replies: 13
    Last Post: 09-12-2001, 09:20 PM

Tags for this Thread