c - 一遍又一遍地在String中使用struct(C语言)

标签 c arrays string struct

我有一个包含多组数据的字符串。我想在它上面使用一个结构来划分数据并能够操作它。到目前为止,我不知道如何操作数组。该数据包含 4 个字符的姓名和年龄。谢谢

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

typedef struct structtest                   
{                                           
 unsigned char name[4];                 
 unsigned char age[2];
} structtest;                               
structtest       *dstr;                     

int main( void )                            
{                                                                                          
    char test[18] = 'mark20john18josh16';   

     //clueless on this part                                   

    return(0);                              
}                                           

最佳答案

通常要将任何内存序列化为字符数组,您需要写入它的大小(例如 LONG32 - 4 个字节),然后写入字符串/内存本身。阅读时 - 首先阅读大小,然后您就知道需要阅读多少。

对于使用 char 数组作为结构: 试试这个:

int i;
char* yourstring = (char*)malloc(N);
YourStruct* p = (YourStruct*)yourstring;
//Do something
while (i < sizeof(yourstring) / sizeof(YourStruct))
{
    ++p;
    //Do something
}

关于c - 一遍又一遍地在String中使用struct(C语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22499637/

相关文章:

c++ - 如何在 C++ 中测试公钥/私钥对?

c - C union 的命名标准

arrays - 为查询返回的每一行调用 set-returning plpgsql 函数

php - WordPress 中的 PHPmailer 是否有更清晰的代码?

c - 运行时错误(堆栈溢出)

c - 缓冲区空终止

arrays - 如何更改数组字典中元素的值?

c - 在C中减去两个字符串

Java整数与字符串相加

string - 从nodejs中的字符串调用函数