1

I recently found a strange thing in C++.

Here's the program

#include<iostream>

void fun(int *)
{
    std::cout << "Strange!!!\n";
}

int main()
{
    int x = 33;
    fun(&x);
    return 0;
}

I know my fun is kind of funky, I'm passing a pointer and not using that thing at all. But that's not the point.

The point is why this thing is even compiling ? I don't have a variable for that pointer in there, and It didn't warn me also. Even it compiles then how the heck I'm going to use that pointer inside fun anyway ? Is it normal ?

Abhisek
  • 4,610
  • 3
  • 17
  • 27
  • 2
    Yes, it's normal. It's mostly used for forward declarations of functions, or to distinguish between the prefix and postfix versions of `operator++` (one gets a dummy parameter), or for sets of related functions that must have the same signature (because they're called through pointers), but some of them don't require a particular parameter. Also, you can't use it without giving it a name. – Kevin Jan 31 '17 at 16:36
  • 1
    It's a totally valid C++ function declaration and definition. – Some programmer dude Jan 31 '17 at 16:37

0 Answers0