Pointers

Jan 29, 2016 at 3:12pm
Program is to enter a string and search for a specific word in that string. Output should be the first alphabet of the word searched for.
I'm gettin errors and I'm not able to fix 'em. 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
27
  using namespace std;
#include<iostream>
#include<string.h>
char *substr(char *string1,char string2);
int main()
{
	char sen[50];
	char search[50];
	char sea[50];
	char*ans=nullptr;
	cout<<"Enter the string"<<endl;
	cin>>sen;
	cout<<"Enter string to be searched for "<<endl;
	cin>>search;
     
    ans=substr(sen,search);
	cout<<ans;
	
}
char *substr(char *string1,char *string2[])
{
	while((*string2!=*string1)&&(*string1))
	{
		string1++;
	}
	return(string1);
}
Jan 29, 2016 at 3:17pm
To begin with. Your function prototype's arguments doesnt match your function definition.

char *substr(char *string1,char string2); // prototype

char *substr(char *string1,char *string2[])
Jan 29, 2016 at 3:21pm
char *substr(char *string1,char string2);
So, substr is declared as a function with two parameters. The first is a pointer-to-char and the second is a single char.

In the definition:char *substr(char *string1,char *string2[])

substr is a function with two parameters. The first is a pointer-to-char and the second is a pointer-to-pointer-to-char, which hopefully you can see does not remotely match how you told the compiler you were going to define the function.

I'm gettin errors and I'm not able to fix 'em. Please help

When you're "gettin errors" post the full text of the (first few) errors with your code.
Topic archived. No new replies allowed.