Re: [links, FAQ] multidimensional arrays and pointers



<junky_fellow@xxxxxxxxxxx> wrote in message
news:1117516970.445477.182870@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Consider a 2-dimensional array,
> char arr[3][3] = { 'a','b','c',
> 'd','e','f',
> 'g','h','i'
> };
>
> what is the difference between,
>
> (arr + 1) and
> *(arr + 1)


The expression '(arr + 1)' yields the address
of the type 'char[3]' object (an array) whose
first element is located three bytes past the
starting address of the array 'arr'. The
expression's type is 'char(*)[3]' (pointer to
array of three characters).

*(arr + 1) yields the address of the type 'char'
object located three bytes past the starting address
of the array 'arr'. The expression's type is
'char *' (pointer to type 'char').

Thus, both expressions yield the same value (the
address of the character object with the value 'd'
in your example); however the two expressions' types
are different ('char(*)[3]' and 'char*', respectively.
(The actual address value of the two expressions will
vary among implementations, and often between different
executions of the program on the same platform)

I hope I didn't just do your classwork for you. :-)

(If I've erred in the above, someone will be sure to
point it out.) :-)

More info about arrays and pointers here:
http://web.torek.net/torek/c/pa.html

Other good information from Mr. Torek on C here:
http://web.torek.net/torek/c/

comp.lang.c FAQ here:
http://www.eskimo.com/~scs/C-faq/top.html

-Mike


.



Relevant Pages

  • Re: Allowing zero-dimensional subscripts
    ... items are formed by comma-separated lists of expressions. ... Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing. ... So there's a regular way to create non-empty tuples, and a subscript ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: fgetc
    ... *a pointer to an array ... defines an array of 10 elements, each element having type "int *". ... If you rewrite the declaration line to ... On entry to the function all size expressions of its ...
    (comp.lang.c)
  • Re: A[x][y][z]
    ... given an array `whatever' the expression ... `whatever' is a pointer. ... "multidimensional" array expressions becomes easy; ... >> it has an enviable elegance. ...
    (comp.lang.c)
  • Re: Allowing zero-dimensional subscripts
    ... The items of a tuple are arbitrary Python objects. ... items are formed by comma-separated lists of expressions. ... the grammar rule used for list subscript is different from the ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: Allowing zero-dimensional subscripts
    ... items are formed by comma-separated lists of expressions. ... So there's a regular way to create non-empty tuples, and a subscript ... But note neither testlist nor subscriptlist can create an empty tuple. ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)