Resizing a char[x] to char[y] at runtime

后端 未结 11 1107
我在风中等你
我在风中等你 2021-02-11 01:33

OK, I hope I explain this one correctly. I have a struct:

typedef struct _MyData
{
   char Data[256];
   int  Index;
} MyData;

Now, I run into

11条回答
  •  迷失自我
    2021-02-11 01:43

    You cant reacolate manualy.

    You can do some tricks wich i was uning when i was working aon simple data holding sistem. (very simple filesystem).

    typedef struct
    {
        int index ;     
        char x[250];
    } data_ztorage_250_char;
    
    typedef struct
    {
        int index;      
        char x[1000];
    } data_ztorage_1000_char;
    
    int main(void)
    {
          char just_raw_data[sizeof(data_ztorage_1000_char)];
          data_ztorage_1000_char* big_struct;
          data_ztorage_250_char* small_struct;
          big_struct = (data_ztorage_1000_char*)big_struct; //now you have bigg struct
          // notice that upper line is same as writing 
          // big_struct = (data_ztorage_1000_char*)(&just_raw_data[0]);
    
          small_struct = (data_ztorage_250_char*)just_raw_data;//now you have small struct
    
          //both structs starts at same locations and they share same memory
         //addresing data is 
          small_struct -> index = 250;
    }
    

提交回复
热议问题