小编And*_*anu的帖子

为指向函数的指针编写getter

我有以下问题:

"list.c"

struct nmlist_element_s {
    void *data;
    struct nmlist_element_s *next;
};

struct nmlist_s {
    nmlist_element *head;
    nmlist_element *tail;
    unsigned int size;
    void (*destructor)(void *data);
    int (*match)(const void *e1, const void *e2);
};

/*** Other code ***/
Run Code Online (Sandbox Code Playgroud)

从结构中返回' 析构函数 ' 的函数的签名是什么?例如,返回' size ' 的函数的签名是:

unsigned int nmlist_size(nmlist *list);
Run Code Online (Sandbox Code Playgroud)

" 析构函数 " 会是什么情况.

c encapsulation

1
推荐指数
1
解决办法
183
查看次数

如何创建一个通用的C自由函数

我有一些与'list'数据结构相关的C结构.它们看起来像这样.

struct nmlist_element_s {
    void *data;
    struct nmlist_element_s *next;
};
typedef struct nmlist_element_s nmlist_element;

struct nmlist_s {
    void (*destructor)(void *data);
    int (*cmp)(const void *e1, const void *e2);
    unsigned int size;
    nmlist_element *head;
    nmlist_element *tail;
};
typedef struct nmlist_s nmlist;
Run Code Online (Sandbox Code Playgroud)

这样我就可以在"nmlist_element-> data"中保存不同的数据类型."构造函数"(以OOP表示)具有以下签名:

nmlist *nmlist_alloc(void (*destructor)(void *data));
Run Code Online (Sandbox Code Playgroud)

其中"析构函数"是取消分配"数据"的特定函数(由nmlist_element保存).

如果我想要一个包含整数作为数据的列表,我的"析构函数"会这样:

void int_destructor(void *data)
{
    free((int*)data);
}
Run Code Online (Sandbox Code Playgroud)

我仍然觉得为每个简单的原始数据类型编写析构函数对我来说是"不友好的".那么写一些这样的东西有诀窍吗?(对于原语):

void "x"_destructor(void *data, "x")
{
    free(("x" *)data);
}
Run Code Online (Sandbox Code Playgroud)

PS:我自己并不是一个宏观迷,在我对C的短暂体验中,除非必要,否则我不会使用它们.

c generics

0
推荐指数
1
解决办法
744
查看次数

返回指向宏中某个值的指针?

是否可以编写一个具有类型和值作为其输入参数 ( MACRO(type,value)) 的宏,并返回一个指向保存所提交的value.

该宏的执行方式应类似于以下函数,但采用更通用的方式:

int *val_to_ptr(int val){
    int *r = NULL;
    r = nm_malloc(sizeof(*r));
    *r = val;
    return r;
}
Run Code Online (Sandbox Code Playgroud)

nm_malloc()故障安全 malloc 在哪里?宏的用法应该与此用法兼容:

printf("%d",*MACRO(int,5));
Run Code Online (Sandbox Code Playgroud)

有可能实现吗?

c macros c-preprocessor

0
推荐指数
1
解决办法
2750
查看次数

使用python minidom解析文档

我有以下XML文档,我必须使用python的minidom解析:

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <bash-function activated="True">
        <name>lsal</name>
        <description>List directory content (-al)</description>
        <code>ls -al</code>
    </bash-function>

    <bash-function activated="True">
        <name>lsl</name>
        <description>List directory content (-l)</description>
        <code>ls -l</code>
    </bash-function>
</root>
Run Code Online (Sandbox Code Playgroud)

这是我试图解析的代码(基本部分):

from modules import BashFunction
from xml.dom.minidom import parse

class FuncDoc(object):
    def __init__(self, xml_file):
        self.active_func = []
        self.inactive_func = []
        try:
            self.dom = parse(xml_file)
        except Exception as inst:
            print type(inst)
            print inst.args
            print inst
Run Code Online (Sandbox Code Playgroud)

不幸的是我遇到了一些错误.这是堆栈跟踪:

<class 'xml.parsers.expat.ExpatError'>
('no element found: line 1, column 0',)
no element found: line 1, column 0
Run Code Online (Sandbox Code Playgroud)

作为一个蟒蛇初学者,请你指出问题的根源.

python parsing dom minidom

0
推荐指数
1
解决办法
9162
查看次数

标签 统计

c ×3

c-preprocessor ×1

dom ×1

encapsulation ×1

generics ×1

macros ×1

minidom ×1

parsing ×1

python ×1