struct转换为数组后如何得到里面的值?

conquer2009 2009-03-26 02:56:01
我有一个结构体:
struct A
{
int size;
int type;
...

}

struct A aa;
char *ptr;
ptr = (char *)aa;

我把它转换为char之后,想读其中type的值,该怎么做啊?(不要转换到struct读)

我是这样做的,但是出错:

char *p;
p = ptr;
p = p + 4;
int type = (int)(*p);

请问正确的该怎么做啊??如何得到type的值啊??help


...全文
307 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghy3274264 2009-03-28
  • 打赏
  • 举报
回复
学习中
Mary1314 2009-03-27
  • 打赏
  • 举报
回复
struct Data
{
int nValue;
int nType;
};

struct Data a;
a.nType = 1;
a.nValue = 100;
char *ptr=(char *)&a;
printf ("%d\n", *((int*)(ptr)));
printf("%d\n", *((int*)(ptr+offsetof(Data, nType))));
Mary1314 2009-03-27
  • 打赏
  • 举报
回复
顶!!!
coldant 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yyunffu 的回复:]
楼上正解
int type = (int)(*p); // 这里有问题, char *p 读出的是一个字节
首先将char型指针转换为int型指针,以求取值时获取的是一个整型值(取出4个字节)。

如果考虑到内存对齐问题,那就比较麻烦了。
[/Quote]

正解
rjianfeili 2009-03-27
  • 打赏
  • 举报
回复
再俩看看
yyunffu 2009-03-27
  • 打赏
  • 举报
回复
楼上正解
int type = (int)(*p); // 这里有问题, char *p 读出的是一个字节
首先将char型指针转换为int型指针,以求取值时获取的是一个整型值(取出4个字节)。

如果考虑到内存对齐问题,那就比较麻烦了。
challenge99 2009-03-26
  • 打赏
  • 举报
回复
int type = (int)(*p); // 这里有问题, char *p 读出的是一个字节

int type = *(int *)p;
traceless 2009-03-26
  • 打赏
  • 举报
回复
int type = (int)(*p);
错在这步,p是char型指针,(int)(*p);只是得到了结构体里type的低8位
需要把p强制转化成int型指针:
int type = *(int *)p;

lyconglove 2009-03-26
  • 打赏
  • 举报
回复
学习....
hylove9494 2009-03-26
  • 打赏
  • 举报
回复
sf
YY_kici 2009-03-26
  • 打赏
  • 举报
回复
typedef struct _tagStu
{
int i;
int j;
} stu;

void main()
{
stu a;
a.i = 10;
a.j = 11;
char *p = (char*)&a;
p += 4;
printf("%d", *p);
}
dongpy 2009-03-26
  • 打赏
  • 举报
回复
char *p;
p = ptr;
p = p + 4;
int type = (int)(*p);
===================================
int type = *(int*)p;
rabbitlzx 2009-03-26
  • 打赏
  • 举报
回复
如果问题比较复杂,可能会由于存在字节对齐问题,建议使用offsetof来获得某成员的相对偏移量,如下:


#include "stdafx.h"
#include "stddef.h"

struct A
{
int size;
int type;
char cz;
float flt;
int nvalue;
};

int _tmain(int argc, _TCHAR* argv[])
{
struct A aa;
aa.size = 12;
aa.type = 23;
aa.cz = 'b';
aa.flt = 125.1234567f;
aa.nvalue = 55;

//get the pointer
char* ptr = (char*)&aa;
size_t offset = offsetof( A, nvalue );
ptr += offset;
int nV = (int)*ptr;

return 0;
}
rjianfeili 2009-03-26
  • 打赏
  • 举报
回复
ptr = (char *)aa;
改成
ptr = (char *)&aa;
ptr+4 指向后面type int 强制转换
rabbitlzx 2009-03-26
  • 打赏
  • 举报
回复

// testCSDN2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

struct A
{
int size;
int type;
char cz;
float flt;
};

int _tmain(int argc, _TCHAR* argv[])
{
struct A aa;
aa.size = 12;
aa.flt = 5.8f;
aa.type = 23;
aa.cz = 'b';

char* ptr = (char*)&aa;

char* p = ptr;
p += sizeof(int);
int tpe = (int)(*p);

return 0;
}


downmooner 2009-03-26
  • 打赏
  • 举报
回复

struct A
{
int size;
int type;
};

int main()
{
struct A aa;
aa.size=8;
aa.type=18;
char *ptr;
ptr = (char *)&aa;
printf("%d",*(int *)(ptr+offsetof(A,type)));
return 0;
}
xiaoQ008 2009-03-26
  • 打赏
  • 举报
回复
ptr = (char *)aa;
楼主这个地方错了哦
应该是ptr = (char *)&aa;
再在ptr所指向的地址上+4、
因为size是int 占了4个字节
+4后再强制转为(int *)
magipan 2009-03-26
  • 打赏
  • 举报
回复
这样做貌似可以额,不过不知其所以然,等待高手~~
struct aa{
int a;
int b;
}bb={1,3};
int main()
{
char *ptr=(char *)&bb;//要加上&符号吧,指针么
printf("%d,%d",*(int *)ptr,*((int *)ptr+1));
baihacker 2009-03-26
  • 打赏
  • 举报
回复
#include <stdio.h>

struct A
{
int size;
int type;
} ;

int main(void)
{
struct A test;
test.type = 0x12345678;
printf("%x\n", *(int*)((char*)&test+4));
return 0;
}
xiaoQ008 2009-03-26
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
struct A
{
int size;
int type;
};
int main()
{
struct A aa;
aa.size=10;
aa.type=20;
char *ptr=(char *)&aa;
printf("%d\n",*((int *)(ptr+4)));
return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧