how to find cell index no. in 2

in C programming if an 2-D array is given like ( int a[5][3]) and base address and address of particular element (cell ) is also given and have to find index no. of that element(cell) (row and col no.) can we find that? if yes how?

i know the formula of finding address is like this

int a[R][C];

address(a[i][j])=ba+size(C*i+ j);

if ba, R,C,Size and address(a[i][j]) is given... how to find value of i and j?

for finding the value of 2 variable we need 2 equation ..but im not able to find 2nd equation.


  • The specific address minus the base address gives you the size in bytes, from the base to the specific address.

  • If you divide that size in bytes with sizeof(ba[0][0]) (or sizeof(int) ), you get the number of items.

  • items / C gives you the first dimension and items % C gives you the second dimension.

  • Thus:

    int ba[R][C];
    uintptr_t address = (uintptr_t)&ba[3][2]; // some random item
    
    size_t items = (address - (uintptr_t)ba) / sizeof(ba[0][0]);
    size_t i = items / C;
    size_t j = items % C;
    

    It is important to carry out the arithmetic with some type that has well-defined behavior, therefore uintptr_t .

    If I had done int* address then address - ba would be nonsense, since ba decays into an array pointer of type int(*)[3] . They aren't compatible types.


    Use integer division and remainder operators.

    If you have the base and a pointer to an element, elt , then there are two things:

  • In "pure math" terms, you'll have to divide by the size of the elements in the array.

  • In "C" terms, when you subtract pointers this division is performed for you.

  • For example:

    int a[2];
    
    ptrdiff_t a0 = (ptrdiff_t)&a[0];
    ptrdiff_t a1 = (ptrdiff_t)&a[1];
    
    a1 - a0; // likely 4 or 8.
    

    This will likely be 4 or 8 because that's the likely size of int on whatever machine you're using, and because we performed a "pure math" subtraction of two numbers.

    But if you let C get involved, it tries to do the math for you:

    int a[2];
    
    int * a0 = &a[0];
    int * a1 = &a[1];
    
    a1 - a0; // 1
    

    Because C knows the type, and because it's the law, the subtracted numbers get divided by the size of the type automatically, converting the pointer difference into an array-like index or offset.

    This is important because it will affect how you do the math.

    Now, if you know that the address of elt is base + SIZE * (R * i + j) you can find the answer with integer division (which may be performed automatically for you), subtraction, more integer division, and either modulus or multiply&subtract:

  • offset or number = elt - base. This will either give you an index (C style) or a numeric (pure math) difference, depending on how you do the computation.

  • offset = number / SIZE. This will finish the job, if you need it.

  • i = offset / R. Integer division here - just throw away the remainder.

  • j = offset - (i*R) OR j = offset % R. Pick what operation you want to use: multiply & subtract, or modulus.

  • 链接地址: http://www.djcxy.com/p/85946.html

    上一篇: 跨源文件进行类型检查

    下一篇: 如何找到细胞索引号。 在2