Quantcast
Viewing all articles
Browse latest Browse all 102

Answer by mikyll98 for What is the error in this simple function returning pointer?

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.


Viewing all articles
Browse latest Browse all 102

Trending Articles