C - array

  • Thread starter sicrayan
  • Start date
  • #1
8
0

Homework Statement


Which is an invalid declaration of an array as a formal parameter of a function?
a)int x[1][2][3]
b)int x[]
c)int x[][12]
d)int x[2][]

Homework Equations


-


The Attempt at a Solution


the answer is d; but why?
 

Answers and Replies

  • #2
33,949
5,603
I vaguely recall that for formal parameters of a function, you can specify all but one dimension of an array, and the one that isn't specified has to be the first dimension. A so-called array parameter of a function is actually a pointer. In the case of answer b, x is the address of (i.e., a pointer to) the first element in a one-dimensional array. In answer c, x is the address of the first row of a two-dimensional array, where each row has 12 elements. In memory, the first row would be 12 ints, then the second row of another 12 ints, and so on. For multi-dimensional array parameters, the compiler needs to know the 2nd index in a two-D array and 2nd and 3rd in a three-D array so that it can figure out how to access the elements in the array.
 
  • #3
8
0
Thank you for your answer.
 

Related Threads on C - array

  • Last Post
Replies
2
Views
1K
  • Last Post
2
Replies
38
Views
3K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
12
Views
2K
  • Last Post
Replies
4
Views
14K
  • Last Post
Replies
5
Views
2K
Replies
8
Views
3K
Replies
16
Views
4K
Replies
2
Views
1K
Replies
7
Views
677
Top