-8

I was going through study materials from my previous year at University, and I saw a question like:

What is the difference between int *a and int a[5] and int *[5]. What does the last one indicate?

Will
  • 24,082
  • 14
  • 97
  • 108
  • 6
    http://stackoverflow.com/questions/27811025/what-is-the-meaning-of-int-pt5-in-c check the last answer of the link – Ankur Jyoti Phukan Dec 27 '15 at 10:35
  • 8
    Questions like this are really not welcome here, because they practically shout: “I didn’t do any research of my own! Spoon-feed me the answers!” Please read the help files about how to ask good questions. – Tom Zych Dec 27 '15 at 10:40
  • http://cdecl.ridiculousfish.com/?q=int+%28*arr%29[5] – arrowd Dec 27 '15 at 11:02
  • 2
    Did you perhaps instead of `int *[5]` meant `int *a[5]`? – alk Dec 27 '15 at 11:55

2 Answers2

0

the int *a[5] declares an array of pointers to int.

the easiest was to determine the specifics of a variable declaration is read it from right to left.

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

In a nutshell:

  • int *a - creates a pointer to an int. This should contain the memory address of another int.

    Example values of *a are 0x00001, 0x000057, etc.

  • int a[5] - creates an array that contains five int elements with each element containing an int values.

    Here's a visualization of the possible values of each element in the array:

    -------------------------
    | Array element | Value |
    -------------------------
    | a[0]          |     1 |
    | a[1]          |     2 |
    | a[2]          |     3 |
    | a[3]          |     4 |
    | a[4]          |     5 |
    -------------------------
    
  • int *a[5] - creates an array that contains five pointer to an int elements which each element containing the memory address of another int.

    Here's a visualization of the possible values of each element in the pointer array:

    -------------------------
    | Array element | Value |
    -------------------------
    | a[0]          | 0x000 |
    | a[1]          | 0x001 |
    | a[2]          | 0x002 |
    | a[3]          | 0x003 |
    | a[4]          | 0x004 |
    -------------------------
    
Sean Francis N. Ballais
  • 2,338
  • 2
  • 24
  • 42