int * numchar() can not return an int

write the function numchr(). the function has two parameters: a const char * s pointing to the first character in a C-style string, and a char c. return the number of times that c appears inside s


HI, my problem is i keep getting the memory location of char * s, how to i fix it so that i will return an int??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int * numchr(const char * s, char c)
{
    
 int  * count = 0;

  while(*s)
  {
     if (*s == c) *count++;   
      
      s++;
  }
    
}
Last edited on
Do insist that function should return int*? Maybe better to return just int?

if it should be int*, than
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int * numchr(const char * s, char c)
{
    int  * count(new int(0));

  while(*s)
  {
     if (*s == c) *count++;   
      
      s++;
  }
   return count;
    
}


But don't forget to free memory by delete when pointer will be unneeded
i don't know what int* is but the function has to to return an int value such as 3..

after i fixed it with your code i still keep getting this "0x370f2c" from my instructor's Junit test.
As I think you don't understand difference between pointers and variables.
this should be help
1
2
3
4
5
6
7
8
9
10
11
12
13
int numchr(const char * s, char c)
{
    int  count(0);

  while(*s)
  {
     if (*s == c)count++;   
      
      s++;
  }
   return count;
    
}
Topic archived. No new replies allowed.