Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem Debugging this Program (http://www.go4expert.com/forums/debugging-program-t6522/)

RyKling 25Sep2007 10:07

Problem Debugging this Program
 
Hello all,
I have been trying to debug and get this last error fixed for the past several hours. The following are the errors I am receiving when debugging:

pattern.c:11: warning: passing argument 2 of ‘index’ makes integer from pointer without a cast
pattern.c: At top level:
pattern.c:31: error: conflicting types for ‘index’

line 11 pertains to if (index(line, "the") >= 0)
line 31 to int index(char s[], char t[])

I am hoping this is a easy fix. But have tried and searched for hours and have not been successful. If you could help lead me in the right direction, I would greatly appreciate it! The program basically searches for a pattern consisting of "the". Then prints the entire line which contains "the".


Code:

Code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE        1000

main()
{
        char line[MAXLINE];

        while (getline(line, MAXLINE) > 0)
                if (index(line, "the") >= 0)
                        printf("%s", line);
}

getline(s, lim)
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);
}

int index(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 (t[k] == '\0')
                        return(i);
        }
        return(-1);
}


DaWei 25Sep2007 17:21

Re: Problem Debugging this Program
 
Where did you get this program? You have a number of issues. This function definition,
Code:

getline(s, lim)
char s[];    <---- here and
int lim;      <---- here
{
        int c, i;
        ...

is an ancient way to specify the parameter types. I'm not sure it's even supported by the standard. Do it like you do the rest:
Code:

int getline (char s[], int lim)
{
    int c, i;
    ....

Now, as to your first error message. "index" is neither defined nor declared before you call it. A C compiler will therefore assume that it takes an int argument and returns an int. You need to either move the definition up before main or put a declaration (prototype) before main, like this:
Code:

int index(char s[], char t[]);
Your second error pertains to the same thing. The compiler has assumed it takes an int and returns an int, so when it finds the definition, it sees a second form and flags the conflict.

You also need to move 'getline' above main, or add a prototype above main.

The compiler does not make multiple passes. It does not read through the file to find the definitions, then read through it again to compile it. Forms must be known in advance.

Now, since this is C, the compiler will also assume that main returns an int, so the code is okay. However, when you move to C++, if you ever do, you will find stronger type safety. Nothing is assumed. You have to specify the return types. Since that's also the preferred way, in C, you should develop the habit now.
Code:

int main (void) // for C
or
int main () // for C++

Again, if you don't use the void keyword in C, the compiler will assume it's an int. In C++, the absence of an argument means void.

RyKling 25Sep2007 18:29

Re: Problem Debugging this Program
 
Thank you so much for your quick response. Greatly appreciate it. I followed your help and was able to get everything running perfectly. I spent hours last night trying to get the program to run and was unsuccessful. Woke up this morning, read your response and was able to fix things and get it working in a few minutes. Thanks, once again, greatly appreciate it!


All times are GMT +5.5. The time now is 22:13.