sort.c(13) : error C2371: 'shellSort' : redefinition; different basic types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlexC
    New Member
    • Apr 2010
    • 9

    sort.c(13) : error C2371: 'shellSort' : redefinition; different basic types

    program in C, shell sort.

    i've got this error : sort.c(13) : error C2371: 'shellSort' : redefinition; different basic types

    Code:
    #include <stdio.h>
    
    int a[6] = {5, 9 , 2, 8, 7, 1};
    int main(){
    	int m;
    	m = 0;
    	shellSort(a, 6);
    	for(m = 0; m<20; m++)
    		printf("\n%d", a[m]);
    
    	return 0;
    }
    void shellSort( int a[],int N){
    	int i, j, inc;
    	int tmp;
    	for(inc = N/2; inc>0; inc /= 2)
    		for(i = inc; i < N; i ++)
    		{
    			tmp = a[i];
    			for(j = i; j>= inc; j -= inc)
    				if(tmp <a[j-inc])
    					a[j] = a[j - inc];
    				else 
    					break;
    			a[j] = tmp;
    		}
    
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Is that the only message you get nothing on line 7?

    You called shellSort at line 7, it wasn't prototyped so the C compiler automatically assigned as a prototype returning int it declared it as

    int shellSort();

    Then at line 15 you define it as

    void shellSort( int a[],int N)

    And you get an error because you have defined it as returning void but the compiler declared it as returning int.

    Predeclare you function at line 2 to avoid this.

    Comment

    • AlexC
      New Member
      • Apr 2010
      • 9

      #3
      Thanks, I need to study so much. I didn't know that.
      this is fixed up code
      Code:
      #include <stdio.h>
      
      int a[6] = {5, 9 , 2, 8, 7, 1};
      int main(){
      	int m;
      	m = 0;
      	printf("Unsorted list:");
      	for(m = 0; m<6; m++)
      		printf("\n%d", a[m]);
      	shellSort(a, 6);
      	printf("\nShell Sorted: ");
      	for(m = 0; m<6; m++)
      		printf("\n%d", a[m]);
      
      	return 0;
      }
      int shellSort( int a[],int N){
      	int i, j, inc;
      	int tmp;
      	for(inc = N/2; inc>0; inc /= 2)
      		for(i = inc; i < N; i ++)
      		{
      			tmp = a[i];
      			for(j = i; j>= inc; j -= inc)
      				if(tmp <a[j-inc])
      					a[j] = a[j - inc];
      				else 
      					break;
      			a[j] = tmp;
      		}
      return 0;
      }

      Comment

      Working...