Home > Mobile >  Redefinition of 'int str_find(char*, char*)' error
Redefinition of 'int str_find(char*, char*)' error

Time:09-14

I am trying to run the following code:

Main C file:

#include <stdio.h>
#include "my_functions.c"
#include "my_functions.h"

int main(int argv, char ** argc) {
    for (int i = 0; i < argv; i  )
        printf("%s\n", argc[i]);
    printf("%d\n", str_find("=","-h=123"));
    printf("%d\n",str_find("xyx", "aaaa"));
    return 0;
}

Included .c file for Main .c file from local Library / my_functions.c file

#include <stdio.h>

#include "my_functions.h"

int str_length(char* mystring){

    int i=0;

    while(*mystring!='\0'){

        i  ;

        mystring  ;

    }

    return i;
}

Included .h file for Main .c file from local Library / my_functions.h file

#ifndef MyFunctions_h
#define MyFunctions_h
#include <stdio.h>

void str_copy(char *destination, char *source){
    while(*source != '\0'){
        *destination = *source; 
        source  ; 
        destination  ;
    }
    *destination = '\0';
}

#endif

int str_find(char* needle, char* haystack) {
    int i = 0;
    int c = 0;
    while(*needle!='\0'){
          i;
          needle;
    }

    needle -= i;

    while(*haystack!='\0'){
          c;
          haystack;
    }

    haystack -= c;

    int k=0;
    int sp = 0;
   
    for(int d=0; d<=c;   d,   haystack)
    {
        if(*haystack==*needle && k==0)
        {
            sp = d;
              k;
              needle;
        }else if(*haystack!=*needle && k != 0)
        {
           needle -= k;
           k = 0;
        }
    }
    
    if(sp==0){
        return -1;
    }else{
        return sp;
    }
}

Here is the error I am receiving in the Output in Visual Studio Code, which comes from the imported my_functions.h file and the int str_find(char* needle, char* haystack) function :

redefinition of 'int str_find(char*, char*)'

I am unable to resolve this issue on my own and need help.

What needs to be done to solve this issue, so my code can run properly?

CodePudding user response:

The problem is that you did not guard declaration of the str_find.
main.c includes my_functions.c that includes my_functions.h and therefore declares str_find.
After that, you include my_functions.h that declares str_find again.

To fix that just move #endif to the bottom of my_functions.h

  •  Tags:  
  • c
  • Related