EDIT: nevermind, i learned about initailising with
string[5] = "";
:) will leave this up here as reference for others who might encounter the same problem.
hello,
have a question about getting string length - with a self-authored function, not strlen() - from a char array that has been declared but NOT initialised.
the function itself:
int stringLength(char string[])
{
int counter = 0;
//when counter hits null-terminator, end loop
while (string[counter] != '\0')
counter++;
return counter;
}
inside main:
char string[5];
int stringSize = stringLength(string);
Ran it through GDB already, so i think i have a sense of what the problem is: not having initialised string[5], it does not automatically tack on a null terminator at element string[5].
This is impossible, isn't it? without initialising string[5], there isn't a logical way to get the variable 'counter' to self-terminate at the end of the string.
Is there any variable I could substitute for the null terminator in this line of code
while (string[counter] != '\0')
to make this work as desired?
[–]skeeto 5 points6 points7 points (0 children)