How to allocate memory for a String Dynamically?

前端 未结 4 1261
梦谈多话
梦谈多话 2021-01-26 15:30

I am trying to read Data from a Text file & storing it inside a structure having one char pointer & an int variable. During fetching data from file I know that there wil

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 16:09

    If you know the position of the first structure, and the position of the second structure, you also know the total length of the first structure (position of second - position of first). You also know the size of the integer part of the structure, and therefore you can easily calculate the length of the string.

    off_t pos1;  /* Position of first structure */
    off_t pos2;  /* Position of second structure */
    
    size_t struct_len = pos2 - pos1;
    size_t string_len = struct_len - sizeof(int);
    

提交回复
热议问题