0

If I am not clear with the question, please have a look at the code below. Why does the test with character work but not the one with integer ? What is this fundamental difference between string literal and array that I'm finding hard to get my head around ?

using namespace std;

void fun(int &x)
{
    cout << x << " okk" << endl;
}

void test (int* &a, int* &b) {
    int* temp = a;
    a = b;
    b = temp;
    //cout << *a << " " << &a << endl;
}

void test (char* &a, char* &b) {

    cout << &a << " " << &b << endl;
    char * temp = a;
    a = b;
    b = temp;
    //cout << *a << " " << &a << endl;
}

int main()
{
    char *s = "help";
    char *t = "me";

    char *u = "help";

    cout << s << " " << t << " " << u << endl;

    /*char *temp = s;
    s = t;
    t = temp;
    */

    test (s,t);

    cout << s << " " << t << endl;

    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    int b[10] = {11,12,13,14,15,16,17,18,19,20};

    cout << *a << " " << *b << endl;

    test (a,b);

    cout << *a << " " << *b;

}
shrinidhisondur
  • 771
  • 1
  • 8
  • 18

4 Answers4

2

An int[] is not an int*, so you cannot pass an int[] where an int*& reference is expected. However, an int[] does degrade into an int* pointer, so you can pass an int[] where an int* is expected. The & reference makes a big difference. When you pass something by reference, that something must match the data type that the reference refers to.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I assume you mean: "Why can I pass a string literal by reference, but not an array".

Well, that question is flawed, because you can, in fact, pass an array by reference. You can also pass a string literal by reference, but your code is not doing that. Your code is passing pointers by reference.

void test (char* &a, char* &b)

This function takes two pointers to char (char*) by reference.

test (s,t);

s and t are both char* (not string literals), so this is a correct call. The other test function:

void test (int* &a, int* &b)

That is also taking pointers (to int) by reference. But you are trying to pass arrays, which are not pointers. For reference, passing string literals (which are arrays of const chars) to the function would look like this:

test("help","me")

And that will not compile with the current overloads you have.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Ok. I get that. But then how is this valid ? void foo (int* a) { } int main () { int a[10]; foo (a); } – shrinidhisondur Mar 25 '14 at 22:30
  • @user2910606: Because `foo` takes the pointer by value, so it can accept r-value pointers. And the array decays to a r-value pointer. But the `test` function takes its arguments by non-const reference, which requires an l-value. – Benjamin Lindley Mar 25 '14 at 22:34
0

In the first char case you're

  1. Instantiating a string literal on read-only memory
  2. Creating a pointer (NOT an array) that points there
  3. Asking for a reference to a pointer for both arguments

so there's no problem.

In the int case you're

  1. Creating an array of integers
  2. Passing the array to a function that expects a reference to a pointer to int.

Array names are often said to have decayed functionalities when passed either directly or with an explicit pointer. Your function is expecting that the array is already decayed into a pointer before being passed to the function.

In synthesis you need to match the type of the signature beforehand when asking for a reference.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

In the first case you are using pointers

char *s = "help";
char *t = "me";

So you may change their value because they are not constant.

Take into account that string literals have types of constant arrays. If you for example try to call

test ("help", "me" );

you would get a compilation error as in the second case considered below.

In the second case you are trying to convert references to arrays to references to pointers. There is no such an implicit and/or explicit conversion. So the compiler issues an error.

You could achieve the same effect as in the first case if you would write

int *p = a;
int *q = b;

swap( p, q );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335