fir <> writes:
> [citation for Tim Rentsch]
>> fir <> writes:
>>
>>
>>
>> > Can I use c arrays in such way tab[-1] or tab[-1][-1] (I mean when index is below zero)?
>>
>> >
>>
>> > Also - I think if try to use some way of optimization
>>
>> > where instead of checking some edge values like
>>
>> >
>>
>> > if(x>=0 && x<500 && y>=0 && y<500)
>>
>> > tab[y][x] = 1;
>>
>> >
>>
>> > alloc some edge ram and do not bother about x=-1 or x=tab_max_x+1
>>
>> >
>>
>> > but here it would be convenient to cast some pointer ot type tab[500][500] 'into' wider_tab[502][502]
>>
>> > in such way that tab[0][0] wolul correspond to wider_tab[1][1] (and tab[-1][-1] to wider_tab[0][0])
>>
>> >
>>
>> > Can I get something like that? (If not I am sad)
>>
>>
>>
>> The short answer is no. If E is any expression of array type,
>>
>> any use of (E)[-1] or even just ((E)-1) is undefined behavior.
>>
>>
>>
>> To get something like what you want, you would have to use
>>
>> an array of pointers, those pointers being of type (int *),
>>
>> and the type of 'tab' being (int **), with tab pointing at
>>
>> the second element of the array (ie, at index [1]). The
>>
>> pointers in the pointer array should point into a large
>>
>> one-dimensional array, eg, 'int tab_space[ 502 * 502 ];'.
Dear Professor:
Please change your settings, or edit your postings, or both, to
get rid of extraneous blank lines.
> what with
>
> int (*tab)[N+2] = (void *)&wider_tab[1][1];
>
> isnt tab here a pointer to "int somechunk[502]" ?
>
> and if so, tab+1 (or tab[1]) woluld be next one "int some[502}" laying
> after previous -
>
> so if it is working -> (tab[j])[i] (or tab[j][i]), it should be
> working the way I would exactly like- so it would be grrt (great I
> mean)
Indexing in C is not defined just in terms of address
calculation. Address calculation is one aspect, but there
are additional constraints, and those constraints must be
observed; otherwise ALL BETS ARE OFF. The approach
described above violates those constraints. ANY approach
using pointer to array types, that attempts to index an
expression of array type with an index value of -1, will
violate the constraints. There is NO WAY to do what you
want to do using regular arrays or pointers to arrays; use
an array of pointers, and use type (int **) for the
accessing identifer ('tab' in the above) instead.
> as to -1 as far as i remember ritche wrote (in c development
> historical note) that this square bracket syntax is only a
> sugar/alias to pointers *(a+5) becomes a[5] here (or even 5[a]
> points the same as denoted in wiki - so it is maybe just
> mechanical trick), so by analogy if *(a-1) works a[-1] should be
> working also
C is now defined by the ISO document on it. Any writing by
Ritchie, however much we might enjoy or appreciate it, is no
longer authoritative.
|