Search code examples
cprototypeconflictincompatibility

Incompatible type for argument and conflicting types


#include <stdio.h>

void copy_arr(double, double, int);
void copy_ptr(double, double *, int);

int main()
{

    double source[5]={1.1,2.2,3.3,4.4,5.5}; 
    double target1[5]={0.0};
    double target2[5]={0.0};
    copy_arr(source, target1, 5);
    copy_ptr(source, target2, 5);
    return 0;
}

void copy_arr(double source[5],double target1[5],int arraysize)
{
    int count=0;
    puts("....copying using array notation.....");
    for(count=0;count<arraysize;count++)
        {
            target1[count]=source[count];
            printf("target 1 is : %d\t", target1[count]);
        }
}

double copy_ptr(double source[5],double *target2,int arraysize)
{
    int count=0;
    puts("....copying using pointer notation.....");
    for(count=0;count<arraysize;count++)
        {
            *(target2+count)=source[count];
            printf("target 2 is : %f\t", *target2);
        }
}

Errors::

error: incompatible type for argument 1 of ‘copy_arr’ copy_arr(source, target1, 5);

error: incompatible type for argument 2 of ‘copy_arr’ copy_arr(source, target1, 5);

error: incompatible type for argument 1 of ‘copy_ptr’ copy_ptr(source, target2, 5);

error: conflicting types for ‘copy_arr’ void copy_arr(double source[5],double target1[5],int arraysize)

error: conflicting types for ‘copy_ptr’ void copy_ptr(double source[5],double *target2,int arraysize)

I checked in internet but everything was mostly about prototypes. Mine are here but still I am getting this error! What is the reason?


Solution

  • you have following prototypes:

    void copy_arr(double, double, int);
    void copy_ptr(double, double *, int);
    

    then you declare those as:

    void copy_arr(double source[5],double target1[5],int arraysize)
    

    and

    double copy_ptr(double source[5],double *target2,int arraysize)
    

    there's a problem. your prototypes take single doubles as arguments, not arrays of doubles. then, in prototype copy_ptr returns nothing, but in declaration returns double. change those to:

    void copy_arr(double[], double[], int);
    void copy_ptr(double[], double *, int);
    ...
    void copy_arr(double source[],double target1[],int arraysize)
    void copy_ptr(double source[],double *target2,int arraysize)