0

I read here: C: differences between char pointer and array that char pointers and char arrays are not the same. Therefore, I would expect these to be overloading functions:

#include <iostream>
using namespace std;

int function1(char* c)
{
    cout << "received a pointer" << endl;
    return 1;
}
int function1(char c[])
{
    cout << "received an array" << endl;
    return 1;
}

int main()
{
    char a = 'a';
    char* pa = &a;
    char arr[1] = { 'b' };

    function1(arr);
}

Yet upon building I get the error C2084: function 'int function1(char *)' already has a body. Why does the compiler seem to consider a char pointer to be the same as a char array?

1

1 Answer 1

0

Because when you pass an array into a function, it magically becomes a pointer.

Your two functions, then, are the same.

The following are literally* identical:

void foo(int arr[42]);
void foo(int arr[]);
void foo(int* arr);

(* not lexically, of course :P)

This historical C oddity is the major reason lots of people mistakenly think that "arrays are pointers". They're not: this is just a bit of an edge case that causes confusion.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.