Re: Accessing via a pointer to 2d array.

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 11/11/03


Date: 11 Nov 2003 16:34:22 GMT

In <3FB0F51F.7020704@ctually.myaddress.com> Thes <not@ctually.myaddress.com> writes:

>Dan Pop wrote:
>>
>> The bug was elsewhere in your code. The value of *any* object, no matter
>> how large and complex, can be safely copied with memcpy.
>>
>Indeed. I've discovered that there is a difference between
>*StoredMatrix[i][j] and (*StoredMatrix)[i][j] which suprised me.
>
>What are the rules in these cases?

The [] operator having higher precedence, *StoredMatrix[i][j] is the same
as *(StoredMatrix[i][j]), which is NOT what you want in order to access
the element on row i, column j. StoredMatrix[i] is already sending you
beyond the array pointed to by StoredMatrix, if i is not 0.

To preserve your sanity, restrict yourself to pointers to "unidimensional"
arrays, as I've shown in my first post in this thread.

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: const void * const x
    ... Dan.Pop@cern.ch (Dan Pop) writes: ... > Yet, memcpy uses its second argument to access the data, so as usual, ... perhaps you'd care to share it with the rest of us. ... Keith Thompson kst-u@mib.org ...
    (comp.lang.c)
  • Re: memcpy() vs. for() performance
    ... >% cat test2.c ... >% diff test.s test2.s ... Which shows that the memcpy version is still at least as good as the ... Dan Pop ...
    (comp.lang.c)
  • Re: const void * const x
    ... Dan.Pop@cern.ch (Dan Pop) writes: ... > Which accesses the data itself, but it still doesn't allow you access to ... points to void, what it points to can't be changed anyway"; ... passing the pointer to memcpy(). ...
    (comp.lang.c)
  • Re: memcpy() vs. for() performance
    ... >> ALWAYS use memcpy, NEVER use for loops, unless you have empirical ... >> evidence that your memcpy() is very poorly implemented. ... When the size is unknown at compile time, the compiler ... Dan Pop ...
    (comp.lang.c)