简体   繁体   English

C编程。如果我需要在头文件(.h)中引用它的实例,如何在.c中隐藏结构的实现

[英]C programming. How to hide implementation of the struct in .c if I need to refer to its instance in header file (.h)

So I am curious how to hide implementation of the struct in .c file if we need to refer to its instance in header file. 所以我很好奇如果我们需要在头文件中引用它的实例,如何在.c文件中隐藏结构的实现。 For example I have the following struct in header file: 例如,我在头文件中有以下结构:

struct List
{
    NodePtr front;    
};

I want to declare NodePtr in .c file to hide it's implementation. 我想在.c文件中声明NodePtr来隐藏它的实现。 In .c: 在.c:

struct Node
{
    void *value;
    struct Node *next;
};

typedef struct Node *NodePtr;

But of course than .h file does not know what NodePtr is.... 但当然比.h文件不知道NodePtr是什么....

How would I do that in the right way? 我怎么能以正确的方式做到这一点?

Something like this should work fine. 像这样的东西应该工作正常。 Note that the definition of struct Node never leaves List.c . 请注意, struct Node的定义永远不会离开List.c

list.h list.h

#pragma once
#include <stdbool.h>

struct List {
    struct Node *front;
};

void list_init(struct List **list);
void list_free(struct List *list);
void list_insert(struct List *list, void *value);
bool list_contains(struct List *list, void *value);

list.c list.c

#include <stdbool.h>
#include <stdlib.h>
#include "list.h"

struct Node {
    void *value;
    struct Node *next;
};

void list_init(struct List **list) {
    *list = malloc(sizeof(**list));
}

void list_free(struct List *list) {
    struct Node *node = list->front;
    while (node != NULL) {
        struct Node *next = node->next;
        free(node);
        node = next;
    }
    free(list);
}

void list_insert(struct List *list, void *value) {
    struct Node *node = malloc(sizeof(*node));
    node->value = value;
    node->next = list->front;
    list->front = node;
}

bool list_contains(struct List *list, void *value) {
    struct Node *node;
    for (node = list->front; node != NULL; node = node->next)
        if (node->value == value)
            return true;
    return false;
}

main.c main.c中

#include <stdio.h>
#include "list.h"

int main() {
    struct List *l;
    list_init(&l);

    int *value_1 = malloc(sizeof(int));
    int *value_2 = malloc(sizeof(int));
    int *value_3 = malloc(sizeof(int));

    list_insert(l, value_1);
    list_insert(l, value_2);
    list_insert(l, value_3);

    printf("Does the list contain value_1: %d\n", list_contains(l, value_1));
    printf("Does the list contain null:    %d\n", list_contains(l, NULL));

    list_free(l);
    return 0;
}

It's very possible that I have some errors in this code. 我很可能在这段代码中有一些错误。 If you see any, feel free to fix them. 如果你看到任何,请随时修复它们。

You have all the right to say: 你有权利说:

typedef struct Node *NodePtr;
struct List
{
    NodePtr front;    
};

this typedef struct Node *NodePtr; 这个typedef struct Node *NodePtr; is a forward declaration . 是一份前瞻性声明 You can use it for as long as you use pointers to types. 只要使用指向类型的指针,就可以使用它。 Pointers need no knowledge about the type's (classes) structure. 指针不需要关于类型(类)结构的知识。 And the compiler is happy. 编译器很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM