#include <iostream>
usingnamespace std;
int stringLenght(constchar*);
int main(){
char *s = "hello pointer\n"; // You need to end it with a null character.
cout<<"the lengh is: "<<stringLenght(s)<<endl;
return 0;
}
int stringLenght(constchar* s){
int c=0;
for(; *(s+c)!='\n'; c++){}
return c;
}
I'm using the for loop that way, because I'm used to...
If you're using this function name: string_length_1, then why are you creating a prototype of a function that doesn't exist: stringLength?
From Internet:
Having the prototype available before the first use of the function allows the compiler to check that the correct number and type of arguments are used in the function call and that the returned value, if any, is being used reasonably.
Also, I put those prototypes just to be able to define the function after the main() function, but if you're defining them before the main function, you don't need a prototype function.
lbgladson The code you posted (in your 2nd post) is nearly correct. You just have some function name issues. After choosing the same name for the function throughout = stringLength I get:
I found that changing the char*'s to const char*'s (in function too) eliminates the 1st 2 warnings. Remove the unused variable s1 from main() and it's perfect!
EDIT: Noticed you need 2 versions. Replace str[counter] with *(str+counter) for that.