Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between int* x[n][m] and int (*x) [n][m]?

Tags:

c++

c

pointers

As I see it int *x[n][m] declares x to be a 2-d array of pointers to integers, so allocating memory should be as easy as x[i][j] = new int and as expected it works fine. Now if I change the declaration to:

int (*x)[n][m]

x[i][j] = new int no longer works and results in a compilation error.

x = (int(*)[n][m]) malloc (sizeof(int[n][m])) however compiles. From the few tests I ran, after the memory allocation, the different declaration/allocation combination doesn't seem to effect the values stored in the variable. Am I missing something? So my question is, **Is there a difference between int *x[n][m] and int (x)[m][n]. How is int (x)[n][m] stored in memory?

like image 582
Rakib Ansary Avatar asked Apr 08 '14 11:04

Rakib Ansary


People also ask

What is the difference between int * x and int * x?

To the compiler, they have exactly the same meaning.

Is int [] and int * Same?

int * means a pointer to an integer in your memory. The [] bracket stands for an array. int a[10]; would make an array of 10 integers. int *a; would make a pointer to an integer.

What is the difference between int * A and int A 5 and int *[ 5 ]?

4 Answers. Show activity on this post. of type integer; Each member of the array can hold the address of an integer. int (*a)[5] - Here "a" is a pointer to the array of 5 integers, in other words "a" points to an array that holds 5 integers.

What is the difference between int * ptr and int * ptr?

there is no difference between int* ptr and int *ptr. but consider you try to create two pointers in a line: int *x, *y; // creates two int pointers int* x, y; //creates an int pointer x but y is "just" an int. Therefore int *x usually results in less errors.


2 Answers

As you can easily discover:

  • int *x[n][m] is a two dimensional array of pointers to int.
  • int (*x)[n][m] is a pointer to a two dimensional array of ints.

The answer to your question is that the first is an array so the memory is 'inline' - it might be static, automatic (on the stack) or on the heap, depending on where you define it.

The second is a pointer to an array and the pointer must be initialised before what it points to is used. Most likely the memory will be allocated on the heap, but it's also possible that it might be a static or auto array defined elsewhere. If you access members of the array before initialising the pointer, you get Undefined Behaviour.

like image 73
david.pfx Avatar answered Oct 11 '22 13:10

david.pfx


int *a[n][m] is a two dimensional array of pointers to int.

int (*p)[n][m] is a pointer to a two dimensional array of ints (it is the type you get by taking the address of int[n][m]).

In both cases, n and m need to be compile time constants, otherwise the declarations are not legal in C++ (but are in C). Your compiler might have an extension to allow it, though.

First one can be used to simulate a three dimensional array. I say simulate, because it would not be a proper array with contiguous storage and the types are different in the first place. In each of the elements of a you can store the address to the first element of an array of integers. Each could have a different size and be allocated dynamically. You can store a pointer to a single (possibly stack allocated) integer, too.

int i = 0;
int a1[2] = {};

int* a2[2][2];
a2[0][0] = a1;  // array to pointer decay here
a2[0][1] = new int[42];
a2[1][0] = new int[84];
a2[1][1] = &i;

p can point to a single 2d array or an array thereof:

int arr[2][3];
int (*p1)[2][3] = &arr;  // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate 42 arrays dynamically
like image 21
jrok Avatar answered Oct 11 '22 14:10

jrok