2
#include<iostream> 
using namespace std;

int main()
{
    char s[] = "Hello";
    char **a = &s;  //Give compilation error
    cout<<a;
}

Since s is a pointer to first element, I should be able to store its address in a pointer to character pointer variable..but it is showing an error.

Ayush Kumar
  • 73
  • 1
  • 8
  • 1
    You try to assign a `char(*)[6]` to a `char**`: When you use `&s` you get a pointer to an array which is, obviously, different from a pointer to a pointer. – Dietmar Kühl Jun 25 '17 at 15:26
  • Since it's not clear exactly what you want to do, why you want `a` to be a pointer to a pointer to `char`, what the actual problem you try to solve is, then we can't really help you more. Please take some time to read about [the XY problem](http://xyproblem.info/) and reflect over how it relates to your question. Then [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), and edit your question to be about the *real* problem. – Some programmer dude Jun 25 '17 at 15:30
  • Thanks...got it... – Ayush Kumar Jun 25 '17 at 15:30

2 Answers2

2

Using the array s can make it decay to a pointer to its first element.

Using plain s when a pointer is wanted is equal to doing &s[0]. That pointer will have the type char *. The expression &s is a pointer to the array and it will have the type char (*)[6]. It's very different.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The expression &s is a pointer to an object of type char[6] which is the type of the array s.

On the other hand the type char ** is pointer type to an object of type char * . The types char[6] and char * are different types. So the pointer types are incompatible.

Your fallacy is that you think that in this expression &s the array designator is implicitly converted to pointer to its first element. However such an assumption is wrong.

It is more explicitly expressed in the C Standard (6.3.2.1 Lvalues, arrays, and function designators) than in the C++ Standard

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

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