8

I was programming a code in C++ when I accidentally put brackets to my pointer and my programs output changed.

Since I am new to programming I wanted to know the difference between these type of pointers:

int* A[n]; 
int (*A)[n];
int *(A[n]);

I have read in my textbook that arrays are also a type of pointers.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 1
    Arrays can *decay* to a pointer to their first element, but it's not a pointer. For example, when you pass an array to a function or use pointer arithmetic this decay happens. – Some programmer dude Feb 22 '16 at 08:40
  • 1
    @JoachimPileborg When you pass an array to a function *whose parameter is a pointer*. It is basically using the name of an array to initialize or assign to a pointer that does it, not passing to a function. – juanchopanza Feb 22 '16 at 08:41
  • learn the clockwise spiral rule and you'll never be confused about complex declarations again. http://stackoverflow.com/a/31789222/3758484 – johnbakers Feb 22 '16 at 09:00
  • appreciate your help guys :) – Lovely Princess Feb 22 '16 at 09:22
  • @juanchopanza; It's not the name but the array itself which decays to pointer to its first element. – haccks Feb 22 '16 at 09:58
  • @haccks Whatever. I didn't say the name decays. I don't even know what that would mean. – juanchopanza Feb 22 '16 at 10:08
  • @juanchopanza; *It is basically using **the name of an array** to initialize or assign to a pointer that does it,...* – haccks Feb 22 '16 at 10:15

2 Answers2

15
int* A[n];

A first and foremost is an array no matter what type the element is. After applying pointer *, we know A is an array of int pointers.

int (*A)[n];

By applying brackets, pointer * has higher precedence over array [] in this case. Then A is first and foremost a pointer no matter what it is pointing to. After applying array [], we know A is a pointer to an array of int.

int *(A[n]);

Brackets won't change any precedence order that would affect the array [], therefore removing brackets would yeild int* A[n] same as your 1st case.

Are array pointers?

No. Array is a datastructure that allocates memory pool and stores the data sequentially where as Pointer points to a particular index in memory pool and references the data stored at that memory location.

Martin Gardener
  • 1,001
  • 8
  • 14
  • Technically speking, arrays *are* pointers in C. – Shark Feb 22 '16 at 08:40
  • 14
    @Shark No, arrays can *decay* to pointers to their first element, but an array is not itself a pointer. – Some programmer dude Feb 22 '16 at 08:41
  • 4
    @Shark `sizeof` would beg to differ. – juanchopanza Feb 22 '16 at 08:42
  • 3
    You are correct, arrays are not pointers. However, in C (or C++), when defining an array the value assigned to the variable for the array is the memory address of the first element in the array (which is a pointer). – Signal Eleven Feb 22 '16 at 08:43
  • @SignalEleven old GCC `std::string` was of size of pointer and contained pointer to the middle of actual string structure. It even could be used with `%s` in `printf`. Does this make `std::string` a pointer? – Revolver_Ocelot Feb 22 '16 at 08:48
  • Thats a much better way of putting it @JoachimPileborg than I did, I agree. I was mainly referring to the old `a[i] = *(a+i)` pointer artithmetic. – Shark Feb 22 '16 at 08:50
  • @signalEleven you are referncing C/C++ as if other languages like Java Python C# have pointers :P – Martin Gardener Feb 22 '16 at 08:59
  • i actually meant that pointers don't actually hold the value rather they point to it accessing/amending that location value. @all – Martin Gardener Feb 22 '16 at 09:00
  • 1
    @Revolver_Ocelot std::string is an object, not an array (which is what the question was about) The question asked was: "also read in my textbook that arrays are also a type of pointers" In C/C++, the bracket notation of arrays are simply shorthand notation for pointer arithmetic, and the value of your array variable is always a pointer to the start of the memory area of the array. – Signal Eleven Feb 22 '16 at 09:05
  • @Danyal Imran :P Yes, it is redundant to mention C/C++ when talking about pointers – Signal Eleven Feb 22 '16 at 09:08
  • @SignalEleven __value__ of variable of array type is simply content of array. Variable of array type decays to pointer with slightest provocation when its name is mentioned, so yes, it can be used where pointers can be used, but it does not make it a pointer. Failure to understand that and decaying rules leads to errors in the future (like the usage of infamous `sizeof` macro and not understanding why it does not always work properly). – Revolver_Ocelot Feb 22 '16 at 09:16
  • thanks alot guys for the relevant information, most of my doubts are cleared cheers. :D – Lovely Princess Feb 22 '16 at 09:23
2

This article contains good examples in reading type declarations in C. http://www.unixwiz.net/techtips/reading-cdecl.html

Basically, you can read out types according to the following precedence:

  1. (Often parenthesized) Inner-most type

  2. Right-most types (Mostly array types: [])

  3. Left-most types, except the outer-most one (Mostly pointer types: *)

  4. Outer-most types (Mostly primitive types: int, char..)

For example, the types you presented can be read out as follows:

int* A[n];  // An array ([n]) of pointer (*) of int.
int (*A)[n];  // A pointer (*) of array ([n]) of int.
int *(A[n]);  // An array ([n]) of pointer (*) of int.

So basically, the first and third type are identical.

Gwangmu Lee
  • 437
  • 3
  • 12