Hi i'm getting an error
CODE :

ERROR: E2356 TEXTSORT.C "TYPE MISMATCH IN REDECLARATION OF 'SWAP'

and i am exhausted trying to fix it...if you could help me please i would appreciate it.

void sort(char* string_array[], int length)
{
    int i;
    int exchange_done;
    do
    {
        exchange_done = 0;
        for (i = 1; i < length; i++)
        {
            if (strcmp(string_array[i], string_array[i-1]) < 0)
            {
                swap(&string_array[i], &string_array[i-1]);
                exchange_done = 1;
            }
        } 
    } while (exchange_done);
}


void swap (char* pStr1, char* pStr2)
{
	char temp;

		  temp = *pStr1;
		*pStr1 = *pStr2;
		*pStr2 = temp; 


}

Change
swap(&string_array, &string_array[i-1]);
to
swap(string_array, string_array[i-1]);

because swap() expects two pieces of 'char*', not 'char **'.

That doesn't do it i'm still getting that same error.


Change
swap(&string_array, &string_array[i-1]);
to
swap(string_array, string_array[i-1]);

because swap() expects two pieces of 'char*', not 'char **'.

Yes, 'type mismatch in redefinition' has nothing to do with how you're calling the function, it pertains to the actual function definition itself. Post your function prototype here, or if you can, figure it out yourself.

I'm not exactly sure but here's my full code and i just can't figure it out.

#include<stdio.h>
#include<string.h>





void sort(char* string_array[], int length)
{
    int i;
    int exchange_done;
    do
    {
        exchange_done = 0;
        for (i = 1; i < length; i++)
        {
            if (strcmp(string_array[i], string_array[i-1]) < 0)
            {
                swap(string_array[i], string_array[i-1]);
                exchange_done = 1;
            }
        } 
    } while (exchange_done);
}


void swap (char** pStr1, char** pStr2)
{
    char* temp;

          temp = *pStr1;
        *pStr1 = *pStr2;
        *pStr2 = temp; 


}

void print_string_array(char* string_array[], int length)
{
    int i;
    for(i=0; i<length; i++)
    {
        printf("%d: %s\n", i-1, string_array[i]);
    }
}


int main()
{
    int length;
    char string1[1000];
    char string2[1000];
    char string3[1000];
    char string4[1000];
    char string5[1000];
    char* string_array[5]={string1, string2, string3, string4, string5};


    printf("\nEnter up to five strings;\n");
    printf("Enter a null string to indicate completion.\n");

    printf("1:");
    scanf("%999s\n", string1);
    if(string1==NULL)
    {
        length=0;
    }
    printf("2:");
    scanf("%999s\n", string2);
    if(string2==NULL)
    {
        length=1;
    }
    printf("3:");
    scanf("%999s\n", string3);
    if(string3==NULL)
    {
        length=2;
    }
    printf("4:");
    scanf("%999s\n", string4);
    if(string4==NULL)
    {
        length=3;
    }
    printf("5:");
    scanf("%999s\n", string5);
    if(string5==NULL)
    {
        length=4;
    }
    else
    {
        length=5;
    }

    if(length==0)
    {
        printf("You did not enter any strings.\n");
        return 1;
    }

    printf("Length of the array is %d\n", length);
    printf("Before sorting.\n");

    print_string_array(string_array, length);
    sort(string_array, length);


    printf("After sorting.\n");
    print_string_array(string_array, length);


    return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

Strange, I tried that code and it compiled without any errors.

Try renaming your swap() function from

void swap (char** pStr1, char** pStr2)
to e.g.
void my_swap (char** pStr1, char** pStr2)

and see if that fixes the problem.

what did you compile it in....i use visual studio and borland and they both give me the same error.

Your suggestion also did not work, but i do thank you for trying

The reason why your code does not compile, is that you are invoking the swap() from the sort() but you haven't declared your sort() at that point.
So, either
1) move the whole swap() function to top of your code so it precedes sort()
or
2) just declare your swap() before the implementation of sort(), i.e.

void swap (char** pStr1, char** pStr2);
void sort(char* string_array[], int length)
{
    // code here ...
}
void swap (char** pStr1, char** pStr2)
{
    // code here ...
}

PS. The reason why it compiled here, is that, actually in the file into which I pasted your code, one extra #include<> pulled in the swap() from the std namespace keeping the compiler happy.

This article has been dead for over six months. Start a new discussion instead.