Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between these type of Pointers? [duplicate]

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.

like image 875
Lovely Princess Avatar asked Feb 22 '16 08:02

Lovely Princess


People also ask

How do you find the difference between two pointers?

The subtraction of two pointers gives the increments between the two pointers. For Example: Two integer pointers say ptr1(address:1000) and ptr2(address:1016) are subtracted. The difference between address is 16 bytes.

What is the difference between single pointer and double pointer?

So, when we define a pointer to a pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers.

What is the difference between pointers and variables?

A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address.


2 Answers

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.

like image 127
Martin Gardener Avatar answered Sep 20 '22 13:09

Martin Gardener


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.

like image 39
Gwangmu Lee Avatar answered Sep 19 '22 13:09

Gwangmu Lee