8

I was working with pointers and came up with a problem. So far I know that when we create an array of any data type, then the name of the array is actually a pointer (maybe static pointer) pointing towards the very first index of the array. correct?

So what I'm trying to achieve is to create another pointer that may hold the address of the array name(i.e a pointer towards another pointer which in my case is the array name)

For Example:

char name[] = "ABCD";  // name holding the address of name[0]
char *ptr1 = name;     // When is this possible?
char **ptr2 = &name;   // Why not this?
                       // It gives an error: cannot convert char(*)[5] to char**

I'm using Code Blocks as IDE.

16
  • 15
    No, No, No. An array is not a pointer. It can decay to a pointer when passed to a function but it is not a pointer. Do not think of arrays as pointers. Commented Feb 25, 2016 at 20:36
  • 1
    So you mean when I wrote char *ptr1 = name; it actually did char *ptr1 = &name[0]; ? and name itself is not a separate pointer variable ? Commented Feb 25, 2016 at 20:40
  • 2
    @nsilent22: What do you mean with "strict"? An array is not a pointer - fullstop. It has different properties and does not behave like a pointer. It is just converted to a pointer to the first element (but not to the array!) for most usages (there are actually three exceptions). To get a pointer to the array, you still use the & operator. Commented Feb 25, 2016 at 20:44
  • 1
    @milevyo But an array is not a pointer. Too many problems come from thinking that they are so we just need to stop thinking that way. Commented Feb 25, 2016 at 21:04
  • 1
    @milevyo Function-to-pointer and array-to-pointer are implicit conversions. Implicit conversion does not imply that a value of one type is a value of a different type. Analogously, you can write int x = 3; float y = x;, but that does not mean that an int is a float. And that names would be something is nonsense. Names don't exist, only the objects they refer to do. Commented Feb 25, 2016 at 21:12

2 Answers 2

11

TL;DR Arrays are not pointers.

In your code, &name is a pointer to the array of 5 chars. This is not the same as a pointer to a pointer to char. You need to change your code to

 char (*ptr2)[5] = &name;

or,

char (*ptr2)[sizeof(name)] = &name;

FWIW, in some cases (example, passing an array as a function argument), the array name decays to the pointer to the first element in the array.

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

11 Comments

instead of using 5 shouldn't we use sizeof(name)?
Two questions: 1) What is TL;DR and 2) Did you mean array of 5 chars?
@NathanOliver Better one, actually :)
@FiddlingBits 1) short answer. 2) my bad, corrected. :)
@Sourav Ghosh char (*ptr2)[5] = &name means ptr2 as a single pointer or [5] specifies ptr2 as an array of 5 pointers ?
|
3

If you want to use pointer-to-pointer, you can use this:

int main(void){
  char name[] = "ABCD";
  char *ptr1 = name;
  char **ptr2 = &ptr1;
  std::cout << *ptr1 << std::endl;
  std::cout << **ptr2 << std::endl;
}

cheers.

1 Comment

Yes, that's one way out of this mess but I was actually trying to understand that why what I am trying to do is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.