I have trouble with getting the correct number of characters displayed when returning this simple function. Does anyone see what I am doing wrong in the function (the cout function outputs -2 instead of 4 which is the lenght of "test")? Thanks:
#include <iostream>
using namespace std;
int mystrlen(char *ptr, int nums); //function prototype for mystrlen
int main() {
char arr[]="test"; // define the array
int i, num;
mystrlen(arr, num); // send arguments to function
cout << "Print string text " << arr << endl;
cout << "Print string lenght: " << num-1;
return 0;
}
int mystrlen(char *ptr, int nums) {
for(i=0; ptr[i]; i++) nums+= i; // count characters in ptr
cout << nums-1 << endl; // correct number of characters (4)!!
return (ptr, nums); // return ptr and nums into arr and num
}