Home > other >  What is the error in this simple function returning pointer?
What is the error in this simple function returning pointer?

Time:01-31

In this problem statement, we have to take an array of numbers from the user, then ask for a number they want to search for and give address of it. I have written the following code but having constant error.
How to resolve it?

#include<stdio.h>
#include<conio.h>
int search(int ,int ,int );
main()
{
   int i,a[i],target,*p,n;
   pritnf("enter number of numbers u want");
   scanf("%d",&n);
   printf("enter numbers you want in array");
   for(i=0;i<n;i  )
   {
       scanf("%d",&a[i]);
   }
   printf("enter the number u are searching");
   scanf("%d",&target);
   p=search(a,n,target);
   if(p==NULL)
   {
       printf("target not found");
   }
   else
   {
       printf("target found at %d",p);
   }
}
int search(int a[],int n,int target)
{
    int i;
    for(i=0;i<n;i  )
    {
        if(a[i]==target)
        {
            return &a[i];

        }

    }
}

ERROR: C:\Users\hp\Desktop\Untitled1.c|26|error: conflicting types for 'search'|

CodePudding user response:

int search(int ,int ,int );

Your declaration

int search(int a[],int n,int target)

does not match your definition. Hence the error.

Either fix that, or define the function before main(). That way there would be no need of any prototypes.

You are also defining the array in main() with an undefined variable. Here:

int i,a[i];

the value of i is indeterminate. Declare the array once you've read the input.

CodePudding user response:

You declared the function as int search(int ,int ,int );, with 3 integer parameters, but the definition presents a pointer to integer (int a[] equals to int* a) and 2 integer parameters.

The function declaration should be int search(int* ,int ,int );


However, your code presents many other problems:

  • it's a good practice to define the main function as int main(void) or int main(int argc, char **argv), making it return an integer value (usually 0 for success and positive or negative values for failure). You might find the following post interesting: What should main() return in C and C ?;

  • the first printf() is spelled wrong: "printnf", which generates an implicit declaration warning, and since a function called printnf isn't declared anywhere, the compilation fails;

  • you're assigning the integer returning value of search() to p, which is a variable of type "pointer to integer", which generates an "implicit int conversion warning";

  • your search function should just return a[i], while currently it's returning &a[i] which is a pointer to integer. That generates an "implicit int conversion warning" too;

  • the ones outlined by Haris in the other answer.

I'd suggest you to turn on the compiler warnings and pay extreme attention to what it tells you, since all those problems could be easily avoided by taking a look at them.

CodePudding user response:

Let's start from the very beginning.

The header <conio.h> is not a standard header and neither declaration from the header is used in the program. So this directive

#include<conio.h>

may be removed.

The function search is declared with three parameters of the type int

int search(int ,int ,int );

That is it does not accept an array.

And the return type of the function is again int instead of a pointer type.

Pay attention to that the function will not change a passed array. So the function declaration will look like

 int * search( const int[] ,size_t , int ); 

The second parameter that specifies the number of elements in the passed array has the unsigned integer type size_t instead of the signed integer type int. That is the number of elements in an array can not be a negative value. So there is no sense to declare this parameter as having a signed integer type.

Also your function returns nothing if the target element is not found in the array.

In this declaration

int i,a[i],target,*p,n;

you declared a variable length array a[i]. But the variable i has not initialized yet. So this declaration is invalid and results in undefined behavior.

And moreover the number of elements in the array is stored in the variable n:

pritnf("enter number of numbers u want");
scanf("%d",&n);

So you need to declare the array after the variable n will get its value.

In this call of printf

printf("target found at %d",p);

there is used invalid conversion specifier %d with a pointer. This invokes undefined behavior.

You need to use at least the conversion specifier %p instead of %d.

And at last the function main shall have the return type int.

int main( void )

The program can look the following way.

#include <stdio.h>

int * search( const int[], size_t, int );

int main( void )
{
    size_t n = 0;

    pritnf( "enter number of numbers u want: " );
   
    if ( scanf( "%zu", &n ) == 1 && n != 0 )
    {
        int a[n]; 
        int target = 0;
        int *p = NULL;

        printf( "enter numbers you want in array\n" );

        for ( size_t i = 0; i < n; i   )
        {
            scanf( "%d", a   i );
        }

        printf( "enter the number u are searching: " );
        
        scanf( "%d", &target );

        p = search( a, n, target );

        if ( p == NULL )
        {
            printf( "target not found\n" );
        }
        else
        {
            printf( "target found at %p\n", ( void * )p );
        }
    }
}

int * search( const int a[], size_t n, int target )
{
    const int *p = NULL;

    if ( n != 0 )
    {
        p = a;

        while ( p != a   n && *p != target )   p;

        if ( p == a   n ) p = NULL;
    }

    return ( int * )p;
}
  • Related