Function and pointer problems
This is a nice little assignment my prof. decided to spring on us out of nowhere, (thanks prof. :rolleyes: ). But anyways what I need to do is
write a function that will search a string for any one of a given set of characters.
The idea is to locate the first character in the source string that matches any of the characters in the chars string. The function then returns a pointer to the place in source where the first match was found.
If none of the characters in source match any of the chars, then a NULL pointer is returned. If either argument in NULL, or either string is empty, then a NULL pointer is returned.
But I'm not allowed to use any of the library string routines (strcpy, strcmp, strlen, index, etc.) just pointers to manipulate them to get
the desired result. I'm still kind of new to both pointers, and functions, but I"m to figure simple and regular ones, like the ones shown in the tutoriols
on the site. But I can seem to figure out more complex ones like this one.
Code:
#include <stdio.h>
//The function Prototype that I have to use
char *find_char( char const *source, char const *chars );
void
char *find_char( char const *source, char const *chars )
{
char i; // using this to go through each string and look for the first
// match
for( i = source; i <= source; i++ )
for( i = chars; i <= chars; i++ )
if (i(source) == i(chars) // try to compare the strings not sure if the
// syntax is correct
return &i(source);
else
return NULL;
}
void main()
{
char myString = abcdef;
char nyString = ttyabn;
char j;
printf("The original strings %s\n", myString, nyString);
*find_char(&myString, &nyString);
printf("The first matching character is %s\n", j);
}
These are the errors I keep getting:
just-test.c:63: error: two or more data types in declaration of `find_char'
just-test.c: In function `find_char':
just-test.c:66: warning: assignment makes integer from pointer without a cast
just-test.c:66: warning: comparison between pointer and integer
just-test.c:67: warning: assignment makes integer from pointer without a cast
just-test.c:67: warning: comparison between pointer and integer
just-test.c:69: error: called object is not a function
just-test.c:69: error: called object is not a function
just-test.c:71: error: syntax error before "return"
just-test.c: In function `main':
just-test.c:81: error: `abcdef' undeclared (first use in this function)
just-test.c:81: error: (Each undeclared identifier is reported only once
just-test.c:81: error: for each function it appears in.)
just-test.c:82: error: `ttyabn' undeclared (first use in this function)
just-test.c:80: warning: return type of 'main' is not `int'
make.exe: *** [just-test.o] Error 1
Execution terminated
I'm still trying to figure this out now but any help anybody can give me will be well apprieciated. :)