Thread: Problem Debugging this Program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    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:
     
    
    #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);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do the functions index and getline have prototypes?

    If they don't then that would be a problem in this case; write them, since functions must be declared before they are called. If they do have prototypes, then please post them since that would be the source of errors.

    Code:
    getline(s, lim)
    char s[];
    int lim;
    {
    K & R style function building is dead, don't use it. Just write the parameters in a ( list ) like you did for index.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    No function index and getline do not have prototypes. I am completely new to C so I am going to have to figure out how to even type the prototypes. Thanks for giving me the right direction though. Am I far from getting this to run?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Am I far from getting this to run?
    I think so, but you still have to make sure that the program does what you expect, so test it on some inputs that you believe are possible.

    Good luck with the prototypes. If you still have problems after a while, post back. Someone will help you.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    index is also the name of a deprecated function in string.h
    char *index(const char *, int);

    So perhaps picking another name for your function would be a good idea.

    Also, prototyping all your functions before you call them would definitely help in the long run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    I was successful in getting the program to compile and run after a great
    deal of time trying to figure out the error. I had to place the
    prototype:

    int index(char s[], char t[]);

    and also

    int main(void)

    in order for the program to work. Thanks for your time and help, greatly
    appreciate it. Its good to see you can get help when needed. Truely appreciate it all your responses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  4. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM