Help with pointers!

Hi guys, I can't seem to count the number of cs in my string. Which parts am I doing it wrongly? By the way, I can't change my function headers as they are given to me as part of an assignment. 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
char *strchr(char *ptr, char d);
int how_many(char *ptr, int c);
char d = 'c';
int main(void)
{
	
	int c=0;
	char string[10]={0};
	char *ptr;
	
printf("Please enter a string: ");
scanf("%s", string);
ptr = string;
printf("There are %d number of letter c in the string. \n", how_many(ptr, c));
return 0;
}

char *strchr(char *ptr, char d)
{
int i=0;
while ((*ptr) != '0')
{
if ((*ptr)== d)
{
	printf("i is %c\n", *ptr);
	i++;
	return ptr;
	break;
}
else
{
ptr++;
}}
if (i==0)
{return 0;}
}

int how_many(char *ptr, int c)
{
	while ((*ptr) != '\0')
	{	
		ptr = strchr(ptr,d);
		ptr++;
		c++;
	}
	return c;
}





You've chosen a more complicated solution than was strictly necessary; rather than have how_many call a function to find the next occurrence of the character, why not simply walk the character array looking for the character?

1
2
3
4
5
6
7
8
9
10
int how_many(char *ptr, int c)
{
	while ((*ptr) != '\0')
	{	
		if (*ptr == d) c++;
		ptr++;
		
	}
	return c;
}

The purpose of function char *strchr(char *ptr, char d) is to give the address pointing to the first 'c'. If there is no c, it will return a null value.

I know your solution seems easier but I was told specifically to write this function and use how_many to gather the total number of character c in the string. Please help!
In your how_many loop, your while test dereferences ptr. You cannot dereference a null pointer.

This line:

ptr = strchr(ptr,d);

can make ptr a null pointer.

I also do not recommend that you increment a null pointer, which you will be doing with the line
ptr++

If you ran your code with a debugger, you would have found this already.

Also, what happens if there are no instances in the string? The while loop goes around at least once, and you increment the counting variable anyway, so you will always miscount.

Last edited on
I was thinking ptr++ would bring the pointer to the address after the first character and the loop would thus help me count the total number of characters by re-calling ptr = strchr(ptr,d).

So at this instance, what should I do now to count the total number of characters?
(Sry, I'm a beginner at pointers)

It can be done that way, but you need to guard against carrying out dereferencing or incrementation on a null pointer.

This will work, but it's hideously ugly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int how_many(char *ptr, int c)
{
  
  
	while (ptr != 0 && (*ptr) != '\0')  // Must not dereference null pointer, relies on short-circuit evaluation
	{	
                ptr = strchr(ptr,d);
                if (ptr != 0)
                {ptr++;
                     c++;
                }
	}
	return c;
}
Thanks a lot! You made my day!
Topic archived. No new replies allowed.