Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "pointer to int" and "pointer to array of ints"

Tags:

c

int main() {     int (*x)[5];                 //pointer to an array of integers     int y[6] = {1,2,3,4,5,6};    //array of integers     int *z;                      //pointer to integer      z = y;     for(int i=0;i<6;i++)         printf("%d ",z[i]);      x = y;     for(int i=0;i<6;i++)         printf("%d ",(*x)[i]);      return 0; } 

Both the above printfs print numbers 1 through 6.
If both "pointer to array of integers" and "pointer to integer" can do the same thing, do they have the same internal representation?
EDIT: This code does give warnings when compiled as pointed out by the answers below, however it does print the values correctly both the time on my x86_64 machine using gcc

like image 233
Sandip Avatar asked Mar 07 '10 20:03

Sandip


People also ask

What is the difference between pointer to array and array to pointer?

Here is a list of the differences present between Pointer to an Array and Array of Pointers. A user creates a pointer for storing the address of any given array. A user creates an array of pointers that basically acts as an array of multiple pointer variables. It is alternatively known as an array pointer.

Is pointer to an array same as pointer to an integer in C?

Pointers and arrays are different, but they are accessed similarly if the pointer is being used to access a block of values. However, the array declares a block of some datatype while a pointer only declares space for itself (the data area needs malloc'd)

How is it different from a pointer to an array?

Array in C is used to store elements of same types whereas Pointers are address varibles which stores the address of a variable. Now array variable is also having a address which can be pointed by a pointer and array can be navigated using pointer.

Is integer array same as int array?

There is a difference at run-time. int[] is an array of primitive int values. Integer[] is an "object" array, holding references to Integer objects. Most important practical difference: int[] cannot hold null values.


1 Answers

Firstly, your code will not compile. The array has type int[6] (6 elements), while the pointer has type int (*)[5]. You can't make this pointer to point to that array because the types are different.

Secondly, when you initialize (assign to) such a pointer, you have to use the & on the array: x = &y, not just a plain x = y as in your code.

I assume that you simply typed the code up, instead of copy-pasting the real code.

Thirdly, about the internal representation. Generally, in practice, you should expect all data pointers to use the same internal representation. Moreover, after the above assignments (if written correctly), the pointers will have the same numerical value. The difference between int (*)[5] and int * exists only on the conceptual level, i.e. at the level of the language: the types are different. It has some consequences. For example, if you increment your z it will jump to the next member of the array, but if you increment y, it will jump over the whole array etc. So, these pointers do not really "do the same thing".

like image 99
AnT Avatar answered Oct 04 '22 10:10

AnT