Why the first programme does not work but second works ? and in second why the output is as it gives?

Viewed 42

Program 1

#include <iostream>
#include<string>

using namespace std;

void fun(const char *a)// passing address of "GeeksForGeeks" by value //
{
cout << "const fun() " << a;
}

void fun(const char *&a){// passing address of "GeeksForGeeks" by reference         //
cout << "const reference fun()" <<a;
}
int main()
{
const char *  ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}

error shown

In function 'int main()':
17:8: error: call of overloaded 'fun(const char*&)' is ambiguous
 fun(ptr);
        ^
17:8: note: candidates are:
6:6: note: void fun(const char*)
 void fun(const char *a)
      ^
11:6: note: void fun(const char*&)
 void fun(const char *&a){
      ^

Program 2

#include <iostream>
#include<string>

using namespace std;

void fun(const char *a)// passing address of "GeeksForGeeks" by value //
{
cout << "const fun() " << a;
}

void fun(const char *&a){// passing address of "GeeksForGeeks" by reference         //
cout << "const reference fun()" <<a;
}
int main()
{
const char * const ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}

Output

const fun() GeeksforGeeks

2 Answers
Related