Thread: need help debugging a program w/ counting function

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    need help debugging a program w/ counting function

    i am trying to write a program that counts the number of times that a character appears in an array

    the code reads:

    Code:
    /********************************************************
     *							*
     * FILE: ch8_ex3.c					*
     * CREATED: 8/8/07					*
     * BY: bpf						*
     *							*
     *  this program will run a function that counts the 	*
     *   number of times that some character appears in a 	*
     *   array. the function will be recursive		*
     *							*
     ********************************************************/
    
    #include<stdio.h>
    #include<string.h>
    
    int appear; /* number of appearances */
    
    main()
    {
      char num_line[100]; /* variable for number input line */
      char num_in; /* variable for pratical number */
      char array[100]; /* variable for main fuction's array input */
      int length; /* variable for main function's length input */
    
      /* input number: */
      (void)printf("number! ");
      (void)fgets(num_line, sizeof(num_line), stdin);
      (void)sscanf(num_line, "%c", &num_in); /* make "number" a single character */
    
      /* input array: */
      (void)printf("array! ");
      (void)fgets(array, sizeof(array), stdin);
    
      /* length = length of array */
      length = strlen(array);
    
      /* run count function */
      (void)count(num_in, array, length);
    
      /* print appear value */
      (void)printf("the number appears %d times\n", appear);
    
      return(0);
    }
    
    /********************************************************
     *							*
     * FUNCTION: counts the number of times that some	*
     *           character appears in a array		*
     * PARAMETERS:						*
     *  number - the number being detected in the array	*
     *  array - the array being considered 			*
     *  length - the length of the array			*
     *							*
     * RETURN: no error (0)					*
     *							*
     ********************************************************/
    
    int count(char number, char array[], int length)
    {
      int index; /* index for array */
      appear = 0;
    
      /* count the appearences */
      for(index = 0; index < length; index++)
        {
          if(array[index] == number)
    	appear++;
        }
    
      return(0);
    }
    when i try to compile this in the terminal, the debugger tells me:
    ch8_ex3.c:60: error: conflicting types for 'count'
    ch8_ex3.c:60: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    ch8_ex3.c:38: error: previous implicit declaration of 'count' was here

    i've tried changing around the type of variable i declare the count function as but it does not change

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need a prototype for funciton count() that defines the functions parameters - when the compiler "guesses" the types from what you call it with, it gets it wrong, and gets "upset" when it sees that you have got different actual parameters.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    what do i need to put in this to make a prototype. i thought that the declaration of a function was were the parameters were defined. like: function( parameter1, parameter2, .....)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The easiest way is just to copy the first line of the function and put it above main, and add a semicolon. For example:
    Code:
    int count(char number, char array[], int length);
    
    int main(void)
    {
    Do likewise for each function you have defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM