Pointer Example: Some problems

Hey guys,so ive my school exams next week and I was practising pointers in my computer.Heres what the program is supposed to do:-

Program to search for a given character inside a string and to print the string
from the point of the match.The function should return nonzero if true,zero if false.

What Im getting confused about is the if statement.Also,the function match() doesnt return anything at all.I was having more errors before,but now I have managed to compile the program.

Heres the program:-

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
#include<iostream>
#include<ctype.h>
#include<string.h>

using namespace std;

char *match(char,char*);

int main()
{
 char str[80],ch,*p;
 
 cout<<"Enter the char to search: ";
 cin>>ch;
 cout<<endl;
 cout<<"Enter a string (max. 80 chars): ";
 cin>>str;
 cout<<endl;

 p=NULL;
 p=match(ch,str);

 if(!*p)
  {
   cout<<"Found the character !!!\n";
   for(char *count=p;*count!='\0';count++)
   cout<<*count<<endl;
  }
 else if(*p)
  cout<<"Not found !!!\n";

 cout<<"Thank you !!!\n";

 return 0;
}
//Neel is a son of a bitch.
//Enter a character: s

char *match(char a,char *str)
{
 while((a!=*str) && (*str))
  str++;
 return (str);
}


Pls help in in answering these two questions.
Thnx in advance :)
the function match() doesnt return anything at all


You really need to get better at describing what happens. Of course it returns something.


cin>>str; will take in characters as far as the first space.


if(!*p) This is testing for a character being true or false. What? What?
So cin>>str; will get the input for str only till the first space?

And the if(!*p) checks if 8p is empty or not. It checks if there is no value being pointed to by p.Maybe I got it wrong???
p is a pointer to char. *p is a char. A single byte of memory. How can that possibly be empty? What does it even mean for a char to be "empty"?
Pls run the code and try for yourself guys.Im trying to understand it myself,and would be much obliged if you guys help me in pointing out where in the code Im wrong.

But what I kno till now is that cin>>str; will input the text into str.
What will be the benefit of me running your code? I can see all the problems in it.

cin >> goes as far as the first space. So now you should have done this:

http://lmgtfy.com/?q=cin+stops+reading+at+first+space

Did you do that?
No,could you write the code in here,the site is too slow for me to load on my browser,i couldnt load it.And thnx for your time Moschops. :)
Topic archived. No new replies allowed.