美文网首页
C语言指针存储连续空间

C语言指针存储连续空间

作者: 何亮hook_8285 | 来源:发表于2022-07-12 15:54 被阅读0次
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    struct  Test{
        int b;
        char a;
    };
    
    int main() {
        //定义结构
        Test test[3]={{100,'a'},{100,'b'},{100,'c'}};
        //将结构体数据转万能指针
        void *voidBuff=(void *)test;
        char *charBuff=(char *)voidBuff;
        //打印指针byte内容
        for(int i=0;i<sizeof(struct  Test)*3;i++)
        {
            printf("%d\n",*charBuff);
            charBuff++;
        }
        //将万能指针存储文件
        FILE  * out=fopen("1.bin","wb");
        fwrite(voidBuff,sizeof(struct Test),3,out);
        fclose(out);
       return 0;
    }
    

    相关文章

      网友评论

          本文标题:C语言指针存储连续空间

          本文链接:https://www.haomeiwen.com/subject/zedabrtx.html