Need some quick help with C

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
I'm trying to create the following function (text from the homework):

2. In another program, write a function, int mystrlen (char * str), which takes a string argument (in C this is a char *) and finds the length of that string. To test your function, you can take your programs from homework 1 that use strlen, and replace it with your new function, mystrlen. You should see the same results.

Now, I've just recently started out with C. I've programmed in Java before, but there are some differences that make things "hard" for me (strings, pointers, etc). Now, I thought my code would work. Turns out it spits out some giant, horribly wrong number for "hello" (definitely doesn't say 5). If someone could help point me in the right direction, or tell me what I'm doing wrong, that'd be great. Here's my code:

#include <stdio.h>

int mystrlen(char *str){
int length;
int i = 0;

for(i; str[ i ] != '\0'; i++){
length++;
}
return length;
}

int main(int argc, char * argv[]){

printf("%d\n", mystrlen("Hello"));
return 0;
}
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: Sc4freak
You forgot to initialise length to zero.

I did originally, same results.

EDIT: Nevermind. I must have changed that before I actually tested it or something. My bad! Thanks :)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I did originally, same results.

So? Think about it and learn from it instead of just moving on. If you never set length = 0, how would length++ ever possibly give you the right value?

If you still had a garbage value it would have just meant you had 2+ bugs instead of 1.

If it worked without adding length = 0 it meant you lucked out and either the chunk of RAM had a 0 in it or the compiler happens to initialize stack variables to 0 for you, which could change in debug vs. release builds, or with a different compiler.


Edit: also, this is a bad idea:

int i = 0;

for( i; str[ i ] != '\0'; i++ )

Since you might later add something in between the two that alters the value of i.

This is safer and slightly easier to read:
int i ;

for ( i = 0 ; str[ i ] != '\0' ; i++ )
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
I really don't see why you didn't do something like this.

int strlen(char* str)
{
int i;
for(i = 0; str[ i ] != '\0'; ++i);
return i;
}

You're already incrementing I, why not return it as well. (though, honestly the compiler most likely will throw out the length variable anyways.)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
^ It's definitely more efficient but easier to get wrong (off-by-one error) and a little harder to trust without working through the logic each time you see it.

I'm biased towards legibility and easier maintenance over "optimal" code since we have code in use dating back to 1999, some of which hasn't been touched in 5+ years.
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
^ :p fine, use length instead of i, I don't care.

For me, it is just as readable either way, my way is just a little more brief.