Sep 8, 2014 at 7:31pm Sep 8, 2014 at 7:31pm UTC
I am trying to write a function to get string length without using any library functions and with using pointers.
I am obviously quite a novice in programming so please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//function declarations.
#include <iostream>
int sizeofstring(char *given);
void main()
{
int answer1 = 0;
char *fivep = "abcde" ;
char *sixp = "abcdef" ;
answer1 = sizeofstring(fivep);
printf("-> %d\n" , answer1);
}
int sizeofstring(char *given)
{
int counter = 0, a = 0;
for (a = 0; given[a] != '/0' ; a++)
{
counter = counter + 1;
}
return counter;
}
I have fixed many problems but it seems like pointer is killing me now...
As soon as I compile it, it will just crash without telling me why.
Help please... All is lost for me.
Last edited on Sep 8, 2014 at 8:12pm Sep 8, 2014 at 8:12pm UTC
Sep 8, 2014 at 7:33pm Sep 8, 2014 at 7:33pm UTC
Take out the "char" in front of "five" on line 11.
Sep 8, 2014 at 7:35pm Sep 8, 2014 at 7:35pm UTC
You sir are a genius! thanks a lot!
Sep 8, 2014 at 7:37pm Sep 8, 2014 at 7:37pm UTC
Line 11, says the compiler. What is the purpose of the word char
on it?
Line 19. '/0'
should give at least a warning. If you do mean null, then use '\0'
Sep 8, 2014 at 7:47pm Sep 8, 2014 at 7:47pm UTC
Line 11 was my mistake I thought I have to clarity what kind of value Im passing.
Line 19 I actually did try to detect null so I can tell where the end of string is
Thank you for trying to help!