AHHH simple 20 line code won't work need help

Sep 8, 2014 at 7:31pm
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 7:33pm
Take out the "char" in front of "five" on line 11.
Sep 8, 2014 at 7:35pm
You sir are a genius! thanks a lot!
Sep 8, 2014 at 7:37pm
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
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!
Topic archived. No new replies allowed.