c c++语言基础关于typedef struct 使用

xiaodong60606 2016-10-13 05:10:49
typedef struct libvlc_instance_t libvlc_instance_t; 这行代码有什么意义?什么的情况下会用到?
其它地方没有对libvlc_instance_t 做任何的定义
...全文
347 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-10-15
  • 打赏
  • 举报
回复
某些情况下,指针只想什么并不重要,只要其实一个指针就行了。 像你这个例子中,libvlc_instance_t 用到的地方,一定是以指针,或者二级指针的方式来使用的。 假如对外定义结构体如下:
typedef struct _myFILE myFILE;
设计这样的对外接口,模拟文件读写的接口:
myFILE *lpFile = myOpen("file.ext", MOF_READ);
if (lpFile != NULL)
{
	char szInfo[] = "Hello!";
	myWrite(lpFile, szInfo, sizeof(szInfo));
	myClose(lpFile);
}
你可以看到,对于使用者而言,lpFile 指向的内容并不重要,其只是一个 handle,用来读写文件关闭文件。 或者设计这样的二级指针接口,将一级指针作为参数传入:
typedef struct _InternalFILE {
	int fd;
	int other1;
	int other2;
	...;
} InternalFILE;
假定代码如下,有一个内部结构,而返回值或参数使用的外部结构,只有一个预声明而没有实体,从而使得信息对外隐蔽,并且无需关心实现细节:
int myCreate(const char *lpPath, myFILE **ppFile)
{
	InternalFILE *pif = malloc(sizeof(InternalFILE));
	if (pif != NULL)
	{
		int fd = open(lpPath, _O_CREATE);
		if (fd != -1)
		{
			pif->fd = fd;
			pif->other1 = ...;
			...;
			*ppFile = (myFILE *)pif;
			return 1;
		}
	}
	return 0;
}
调用如下:
mmyFILE *lpFile;
if (myCreate("file.ext", &lpFile))
{
	char szInfo[] = "Hello!";
	myWrite(lpFile, szInfo, sizeof(szInfo));
	myClose(lpFile);
}
这种情况下,其返回的方式与第一个有所不同,但最终还是以一级指针作为传入的方式来使用。 这两种情况,myFILE 指针对外就像 HWND 一样不可见。只要有值,就可以拿来用,无需关心内部实现。
AlbertS 2016-10-13
  • 打赏
  • 举报
回复
在c里如果这样定义结构体struct libvlc_instance_t 。。。 以后声明变量只能struct libvlc_instance_t a1; 如果这样定义了typedef struct libvlc_instance_t libvlc_instance_t; 那么定义变量就可以libvlc_instance_t a2了 在C++里就无所谓了,不用typedef 也可以直接用
paschen 版主 2016-10-13
  • 打赏
  • 举报
回复
http://en.cppreference.com/w/cpp/language/typedef
paschen 版主 2016-10-13
  • 打赏
  • 举报
回复
typedef struct libvlc_instance_t libvlc_instance_t; 为struct libvlc_instance_t 定义一个别名libvlc_instance_t 使用的时候可以直接 libvlc_instance_t xxx 而不需要 struct libvlc_instance_t xxx C++语言里不写typedef本身也可以直接使用前者
  • 打赏
  • 举报
回复
C里面struct变量必须 struct type xxx; 这么typedef 之后就只需要type xxx;就可以了
赵4老师 2016-10-13
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选查找所有引用。
赵4老师 2016-10-13
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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