怎么输出结构体成员指针所指向的数据

C++语言 码拜 7年前 (2017-05-08) 3336次浏览
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
struct HHHHH
{
	HHHHH* pNext;
	void* unused;
	void* value;
};
HHHHH *gpGloable=NULL;
void main()
{
	//
	//////////////////////////////////////////////////////////////////////////
	unsigned int a=100;
	unsigned int *p=NULL;
	p=&a;
	unsigned int **q=NULL;
	q=&p;
	cout<<a<<"\t"<<*p<<"\t"<<p<<"\t"<<**(&p)<<endl;
	printf("%d\n\n",*p);
	//
	//////////////////////////////////////////////////////////////////////////
	HHHHH ss;
	ss.unused=&a;
	unsigned int xx=(unsigned int)(*&(ss.unused));
	cout<<hex<<setw(8)<<xx<<endl;
	cout<< (int*)(ss.unused) <<endl; // 本人想输出unused所指的内容100,但是不行
	printf("%d\n\n",* &(ss.unused)); // 本人想输出unused所指的内容100,但是不行
    cout<< (char*)ss.value <<endl;  //却能输出value所指的内容,why--与上34对比
	printf("%s\n\n", ss.value);     //却能输出value所指的内容,why--与上36对比
}

请看程序的第34、36行,本人就想在屏幕上输出ss.unused所指的能容,就是100。但是怎么都输不到屏幕上!
本人真怀疑本人到底学没学过C/C++?
还请指点,怎么将ss对象中unused成员所指的内容输出到屏幕中。
如能解答,再贴另外一半

解决方案

6

改为:
printf(“%d\n\n”,* ((int*)(ss.unused)));

14

#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
struct HHHHH
{
	HHHHH* pNext;
	void* unused;
	void* value;
};
HHHHH *gpGloable=NULL;
void main()
{
	//
	//////////////////////////////////////////////////////////////////////////
	unsigned int a=100;
	unsigned int *p=NULL;
	p=&a;
	unsigned int **q=NULL;
	q=&p;
	cout<<a<<"\t"<<*p<<"\t"<<p<<"\t"<<**(&p)<<endl;
	printf("%d\n\n",*p);
	//
	//////////////////////////////////////////////////////////////////////////
	HHHHH ss;
	ss.unused=&a;
	unsigned int xx=(unsigned int)(*&(ss.unused));
	cout<<hex<<setw(8)<<xx<<endl;
	cout<< "test"<<*(int*)(ss.unused) <<endl; // 输出的是16进制100(64)
	printf("%d\n\n",*(int*)(ss.unused)); // 本人想输出unused所指的内容100,但是不行
	cout<< (char*)ss.value <<endl;  
	printf("%s\n\n", ss.value);     
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么输出结构体成员指针所指向的数据
喜欢 (0)
[1034331897@qq.com]
分享 (0)