-3
#include<iostream.h>
#include<conio.h>

void print(char *str){
    cout<<str;
}

int main(){
    clrscr();
    char str[]="abcdef";
    print(&str);
    getch();
    return 0;
}

Error
1. Cannot convert 'char[7]' to 'char *'
2.Type mismatch in parameter 'str' in call to print(char *)

Since the parameter list of function print consists of a pointer, then passing &str in function call should be correct
Also if I remove the '&' the program runs fine (even though the print function requires a character reference).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
abhishek gupta
  • 359
  • 1
  • 12

4 Answers4

7

Since the parameter list of function print consists of a pointer, then passing &str in function call should be correct

That's not exactly true: it is not sufficient to pass just any pointer, it needs to be of the correct type. &str is a pointer to an array, while you need a pointer to a single array element.

Since arrays in C++ "decay" to pointers when you pass them to functions, all you need to do is removing the ampersand:

print(str);

if I remove the & the program runs fine (even though the print function requires a character reference)

That's right! An array name (in this case, str) is implicitly converted to a pointer, which is equal to the pointer to array's initial element. In other words, it's the same as writing

print(&str[0]);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Is pointer to an array not same as pointer to first element of array? – abhishek gupta Jul 13 '15 at 18:57
  • @abhishekgupta Numerically, a pointer to an array `&str` is often the same as the pointer to the initial element `&str[0]`. However, the compiler considers it a pointer of different type for the purposes of pointer arithmetic, so it disallows implicit conversion. The name of an array, however, can be converted to a pointer to array element implicitly, so in `char *p = str` the value of `p` matches `&str[0]` both numerically and by type. – Sergey Kalinichenko Jul 13 '15 at 19:01
  • Can you explain "the compiler considers it a pointer of different type for the purposes of pointer arithmetic, so it disallows implicit conversion ". – abhishek gupta Jul 13 '15 at 19:07
  • @abhishekgupta Adding `N` to a pointer `p` advances it by the `N*sizeof(*p)` numerically. For example, adding `1` to `&str` is not the same as adding `1` to `&str[0]`. Here is a quick [demo](http://ideone.com/4phvOW). – Sergey Kalinichenko Jul 13 '15 at 19:13
1
print(&str);

is wrong since the type of &str is char (*)[7] while the expected argument type is char*.

To illustrate the difference between the two types:

char str[]="abcdef";
char (*ptr1)[7] = &str;   // Take the address of the array.
char* ptr2 = str;         // Use the array variable. It decays to a pointer

*ptr2 = 'A';        // OK.
*ptr1 = 'A';        // Not OK.

(*ptr1)[0] = 'A';   // OK
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Just pass str directly to print() like so:

// this already provides std::cout
#include<iostream.h>
// this is unnecessary
//#include<conio.h>

// you could do this to avoid std:: everywhere:
//using namespace std;

void print(char *str){
    // don't forget the namespace:
    std::cout<<str;
}
int main(){
    clrscr();
    char str [] = "abcdef";

    // no "&", just pass str
    print (str);

    getch();
    return 0;
}

Arrays in C/C++ are just pointers to the first element of the array.

http://www.cplusplus.com/forum/articles/9/

erapert
  • 1,383
  • 11
  • 15
0

Function print is declared as having a parameter of type char *

void print(char *str){
    cout<<str;
}

Within the main it is called with an argument of type char ( * )[7] because it is this type that expression &str has.

char str[]="abcdef";
//...
print(&str);

You should call the function simply like

print( str );

In this case the array is converted to pointer to its first character and expression str has type char *

When you have an array of type T as for example

T a[10];

then the array used in expressions is converted to pointer to its first element. For example this

T *ptr = a;

is a correct declaration.

On the other hand if you are using expression &a then the correct declaration for a pointer will look like

T ( *ptr )[10] = &a;

That is in this case ptr is a pointer to an object of type T[10]

And I advice to use a more modern compiler.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335