gpt4 book ai didi

c - 编译单元中符号的可见性级别 - C

转载 作者:太空宇宙 更新时间:2023-11-04 05:16:55 24 4
gpt4 key购买 nike

在下面的代码中,

/**********linkedListImpl.c ***********/

#include"list/list.h"

#if defined(LINKED_LIST)



/***************** Representation - start ******************/
/* struct members are not visible to other .c files */
static struct DListNode{

void *item;
struct DListNode *next;
struct DListNode *prev;
};

/* Should be used in this .c file, only, so static */
static typedef struct DListNode DListNode;
static DListNode* createNode(void *);


static struct List{

DListNode *head;
int size; /*size attribute is not part of list definition, but quick way
to help user code */
}List;


.....
#endif

/************ list.h ************/
#ifndef LIST_H /* Header guard */
#define LIST_H
#include"type.h"

/***************** Usage-start ************/

#if defined(ARRAY) || (LINKED_LIST)

typedef struct List List;

#else
#error "Wrong list implementation macro name !!!"
#endif

...
#endif /* LIST_H */

List类型在 list.h 中声明, 使用 static说明符,

  typedef struct List List;

并在 linkedListImpl.c 中定义, 使用 static说明符,

 static struct List{

DListNode *head;
int size;
}List;

目的:制作List用户可用的符号 ( main.c ) 只能通过指针 ( List* )。用户( main.c )不应该能够访问 List main.c 中的成员.

linkedListImpl.c符号 DListNode使用 static 定义说明符,

  static struct DListNode{

void *item;
struct DListNode *next;
struct DListNode *prev;
};

static typedef struct DListNode DListNode;

用途:用于符号DListNode , 用户 ( main.c ) 应该既不能访问 DListNode main.c 中的成员也无法访问 DListNode对象通过 DListNode*指针。

这是隐藏符号的正确方法吗 List & DListNode

备注:Here是完整代码

最佳答案

是的,就是这个。它被称为不透明指针,用于隐藏接口(interface)中的实现细节。

您可能对 What is an opaque pointer in C? 感兴趣

关于c - 编译单元中符号的可见性级别 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com