Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array syntax in C

Tags:

arrays

c

What is the difference between

int * a[10];

and

int (*b)[10];

I know that the first one is an array of pointers to integers, but what is the second one? If I try assigning

int (*c)[10] = a;

what am I actually doing to c?

like image 287
Keshav Saharia Avatar asked Oct 31 '11 04:10

Keshav Saharia


2 Answers

See if you can install the cdecl command for your system. (On Ubuntu, sudo apt-get install cdecl.) There's also a web interface at cdecl.org.

Here's what it told me for your examples on my system:

$ cdecl
Type `help' or `?' for help
cdecl> explain int * a[10];
declare a as array 10 of pointer to int
cdecl> explain int (*b)[10];
declare b as pointer to array 10 of int
cdecl> 
like image 58
Keith Thompson Avatar answered Nov 14 '22 01:11

Keith Thompson


Second one is a pointer to an array of 10 integers. Where? God knows; you never initialized it.

If you assign a to it, you're making it point to the same array of 10 integers that a pointed to... nothing fancy.

like image 34
user541686 Avatar answered Nov 14 '22 02:11

user541686