使用C中的X和Y值将缓冲区读取为矩阵

使用C中的X和Y值将缓冲区读取为矩阵,c,matrix,int,unsigned-char,C,Matrix,Int,Unsigned Char,我有一个包含256个整数的缓冲区,因此创建一个16x16矩阵。我试图完成的是读取存储在缓冲区中的值,就像它是一个矩阵一样。 如果我给出5和3的坐标,我应该得到Y是5,Y是X的值 例如,这里是缓冲区的一小部分,但在16x3矩阵中便于阅读,并考虑这是一个起始索引0,而不是1) 因此,如果我试图得到Y=2,X=5的值,我应该返回值9 这是我已经掌握的一些代码,但是我的数学不好 unsigned char getTablevalue(int tableIndex, unsigned char *buff

我有一个包含256个整数的缓冲区,因此创建一个16x16矩阵。我试图完成的是读取存储在缓冲区中的值,就像它是一个矩阵一样。 如果我给出5和3的坐标,我应该得到Y是5,Y是X的值

例如,这里是缓冲区的一小部分,但在16x3矩阵中便于阅读,并考虑这是一个起始索引0,而不是1)

因此,如果我试图得到Y=2,X=5的值,我应该返回值9

这是我已经掌握的一些代码,但是我的数学不好

unsigned char getTablevalue(int tableIndex, unsigned char *buffer) {
    return buffer[tableIndex];
}

void getInput(....) {
    int yValue = 2;
    int xValue = 5;
    int returnValue = 0;
    unsigned char *buffer = malloc(256 * sizeof (unsigned char));
    memset(buffer, 0, 256);

    ... Some code to fill buffer...
    returnValue = getTablevalue({what can I put here}, buffer); 

}

在此方面的任何帮助都将不胜感激!提前感谢。

这演示了将缓冲区用作数组,并将缓冲区“数组”复制到实际数组中

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int x, y;
    int arr[16][16];
    int xtest =2, ytest =12; //'random values to test that it works

    /* allocate space for a buffer of 256 values in the range 0 to 255) */
    unsigned char *buffer = malloc(256 * sizeof (unsigned char));

    /* fill the buffer with sequential values from 0 to 255 */
    for (n = 0; n < 256; n++) {
        buffer[n] = n;
    }

    /* just to check the fill was OK - print out the buffer contents */
    for (n = 0; n < 256; n++) {
        printf("%d .. ", buffer[n]);
    }
    printf("\n\n");

   /* fill a 16 * 16 array with values from the buffer
       that the buffer data is arranged in 16 groups of 16 values in a 
       single sequential buffer */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            arr[x][y] = buffer[(x * 16) + y];
        }
    }

    /* print out the array */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            printf("%d\t", arr[x][y]);
        }
        printf("\n");
    }
    printf("\n\n");

    /* just print a 'random' xy value from the matrix and from the buffer
       they will be the same (we hope) */
    printf("X=%d,Y=%d, Matrix[%d][%d] is: %d ... and Buffer %d,%d is %d\n",
            xtest, ytest, xtest, ytest, arr[xtest][ytest],
            xtest, ytest, buffer[(xtest * 16) + ytest]);
    if (arr[xtest][ytest] == buffer[(xtest * 16) + ytest]) {
        printf("Wow - they ARE the same\n");
        } else {
        printf("Oh No - they are different\n");
    }
}    
#包括
#包括
int main()
{
int n;
int x,y;
int arr[16][16];
int xtest=2,ytest=12;/'随机值来测试它是否工作
/*为范围为0到255的256个值的缓冲区分配空间)*/
unsigned char*buffer=malloc(256*sizeof(unsigned char));
/*用0到255之间的顺序值填充缓冲区*/
对于(n=0;n<256;n++){
缓冲区[n]=n;
}
/*只是为了检查填充是否正常-打印出缓冲区内容*/
对于(n=0;n<256;n++){
printf(“%d..”,缓冲区[n]);
}
printf(“\n\n”);
/*用缓冲区中的值填充16*16数组
缓冲区数据以16组16个值的形式排列在
单顺序缓冲器*/
对于(x=0;x<16;x++){
对于(y=0;y<16;y++){
arr[x][y]=缓冲区[(x*16)+y];
}
}
/*打印出数组*/
对于(x=0;x<16;x++){
对于(y=0;y<16;y++){
printf(“%d\t”,arr[x][y]);
}
printf(“\n”);
}
printf(“\n\n”);
/*只需从矩阵和缓冲区打印“随机”xy值
他们将是一样的(我们希望)*/
printf(“X=%d,Y=%d,矩阵[%d][%d]是:%d…而缓冲区%d,%d是%d\n”,
xtest,ytest,xtest,ytest,arr[xtest][ytest],
xtest,ytest,缓冲区[(xtest*16)+ytest]);
如果(arr[xtest][ytest]==缓冲区[(xtest*16)+ytest]){
printf(“哇-他们是一样的\n”);
}否则{
printf(“哦,不,它们是不同的”\n”);
}
}    
样本输出
X=2,Y=12,矩阵[2][12]是:44。。。缓冲区2,12是44

哇-它们是相同的

将列乘以16,再加上行。这是您的索引。
yValue*16+xValue
。当然,这完全取决于你是否正确地填充了缓冲区。为什么不使用2D数组呢?对索引进行一些调整,这正是我想要的。多谢各位。有时候基本的东西会被忽略…你真是太棒了,谢谢。我能够采纳其中一些东西。从缓冲区读取或移动到阵列,会对性能造成更高的影响。对于特定的输入,将调用256次检查值(以获取每个值)。我不知道是否有一个是最好的。然而,关于传统数组有很多可用的信息,包括如何将它们传递给函数以及如何访问数组——不仅通过“[]”下标,还通过操纵指向数组的指针,所以我当然会选择传统数组——但这都是很好的学习。如果你认为我的答案有用,请给它一个+1或接受-或不接受-你的电话。
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int x, y;
    int arr[16][16];
    int xtest =2, ytest =12; //'random values to test that it works

    /* allocate space for a buffer of 256 values in the range 0 to 255) */
    unsigned char *buffer = malloc(256 * sizeof (unsigned char));

    /* fill the buffer with sequential values from 0 to 255 */
    for (n = 0; n < 256; n++) {
        buffer[n] = n;
    }

    /* just to check the fill was OK - print out the buffer contents */
    for (n = 0; n < 256; n++) {
        printf("%d .. ", buffer[n]);
    }
    printf("\n\n");

   /* fill a 16 * 16 array with values from the buffer
       that the buffer data is arranged in 16 groups of 16 values in a 
       single sequential buffer */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            arr[x][y] = buffer[(x * 16) + y];
        }
    }

    /* print out the array */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            printf("%d\t", arr[x][y]);
        }
        printf("\n");
    }
    printf("\n\n");

    /* just print a 'random' xy value from the matrix and from the buffer
       they will be the same (we hope) */
    printf("X=%d,Y=%d, Matrix[%d][%d] is: %d ... and Buffer %d,%d is %d\n",
            xtest, ytest, xtest, ytest, arr[xtest][ytest],
            xtest, ytest, buffer[(xtest * 16) + ytest]);
    if (arr[xtest][ytest] == buffer[(xtest * 16) + ytest]) {
        printf("Wow - they ARE the same\n");
        } else {
        printf("Oh No - they are different\n");
    }
}