Hi, I'm learning about basic functions and trying to debug my code but I'm pretty sure it matches my book exactly. I've looked it over a few times and it looks ok but I'm still getting errors...so if anyone could give it another look that would be a big help. I'm compiling this on a Mac. Here's the code:
Code:
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int max);
int strindex(char source(), char searchfor[]);
char pattern[] = "ould"; /* pattern to search for */
/* find all lines matching pattern */
main()
{
char line[MAXLINE];
int found = 0;
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0)
{
printf("%s", line);
found++;
}
return found;
}
/* getline: get line into s, return length */
int getline(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++)
{
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}
and the errors are:
Code:
strindex.c: In function 'main':
strindex.c:16: warning: passing argument 1 of 'strindex' from incompatible pointer type
strindex.c: At top level:
strindex.c:40: error: conflicting types for 'strindex'
strindex.c:5: error: previous declaration of 'strindex' was here
Thanks for any help!