How to store byte arrays in C?

老子叫甜甜 提交于 2019-12-02 06:54:40

You can allocate 12*96 byte of contiguous memory having 12 rows & 96 columns

char*   image = (char*)malloc(sizeof(char)*12*96);

also, a global array will do just fine

char image[12][96];

From what I understand, you are recieving data row wise ie 96 bytes at a time:

char rcvd_data[96]={0};

and access/set like this::

for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
   rcvd_data= recv_function(); //whatever your recv function is
   for(col=0;col<96;col++) //to point to the col in that row (0-95 col)
   {
      *(image + row*96 + col)= rcvd_data[col]//whatever value you want to assign 
    }
}

and to transfer all 96 bytes in one go:

for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
   rcvd_data= recv_function(); //whatever your recv function is
   memcopy((image + row*96), rcvd_data, 96);
}

The simplest solution to allocate the array you describe is:

uint8_t image[12][96];

However, based on your description " Each one should be 'appended' to the last, as they should end up forming sequential space in memory", that suggests you actually want:

uint8_t image[12 * 96];

and then you will just write your 12 transmission sequentially into that array.

This code you wrote:

for (int i = 0; i < 12; i++) {
    image[byteNum][i] =  receivedImage->value[i]->uint8_t;
}

isn't right, uint8_t is a data type, not a field name.

Also, probably you want to do something with image[i], rather than image[byteNum][i] although I can't be more specific without knowing what the TupleType is and how much you're expecting to find in each Tuple.

One thing I will add to this is be careful using just a char. Use something like unsigned char,signed char, or uint8_t when dealing with pure byte data. While a char is one byte I've seen loss of data because of it's use.

I think you need to malloc two times.

char** image = (char**)malloc(sizeof(char*)*12);
int i = 0;
for(; i < 12; ++i){
   image[i] = (char*)malloc(sizeof(char)*12);
}

You can then treat image as a two-dimension array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!