I am trying to figure out pointer increment here...with method that finds length
I run the program and i get a scrolling mess of characters flying by till program crashes..lol
Code:
#include <iostream>
using namespace std;
int length( char *c ) {
int cnt=0;
if(c) //check to make sure pointer isnt null.
for(;c++;cnt++) //keep running till reach null \0, increment the cnt variable each time.
cout << *c;
else
return 0;
c-=cnt;
return cnt;
}
int main()
{
char *st = "hello world";
cout << endl <<length(st);
return 0;
}