将值传递给 C 中的函数

发布于 2024-11-29 23:58:09 字数 1318 浏览 0 评论 0原文

我是 C 语言新手,已经研究了两个月了。我有一个如下所示的结构:

struct profile_t
{
    unsigned char length;
    unsigned char type;
    unsigned char *data;
};

typedef struct profile_datagram_t
{
    unsigned char *src;
    unsigned char *dst;
    unsigned char ver;
    unsigned char n;
    struct profile_t profiles[MAXPROFILES];
} header;

header outObj;

现在该结构元素内的值被读取为 outObj.src[i]outObj.dst[i]outObj.profiles[i].type

现在我想调用一个函数并将我读取的值传递给一个实际上是 Berkeley DB 的函数。

void main()
{
    struct pearson_record {
        unsigned char src[6];
        unsigned char dst[6];
        unsigned char type;
    };

    DB *dbp;
    int ret;

    if ((ret = db_create(&dbp, dbenv, 0)) !=0)
        handle_error(ret);

    if ((ret = dbp->open(dbp, NULL, "pearson.db", NULL, DB_BTREE, DB_CREATE, 0600)) !=0)
        handle_error(ret);

    const DBT *pkey;
    const DBT *pdata;

    struct pearson_record p;
    DBT data, key;

    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    memset(&s, 0, sizeof(struct pearson_record));

现在,上面的代码是通过查看数据库参考指南中的示例来编写的。但我不明白什么是const DBT。他们还使用 memcopy 添加了结构内部的值,我知道这是正确的方法,但现在我想 memcopy 上面提到的传递的值并将它们存储在结构pearson_record。我该怎么处理这个?任何形式的帮助将不胜感激。

I am new to C and working on it since two months. I have a structure shown below:

struct profile_t
{
    unsigned char length;
    unsigned char type;
    unsigned char *data;
};

typedef struct profile_datagram_t
{
    unsigned char *src;
    unsigned char *dst;
    unsigned char ver;
    unsigned char n;
    struct profile_t profiles[MAXPROFILES];
} header;

header outObj;

Now the values inside the elements of the structure are read as outObj.src[i], outObj.dst[i], and outObj.profiles[i].type.

Now I want to call a function and pass the values read by me to a function which is actually a Berkeley DB.

void main()
{
    struct pearson_record {
        unsigned char src[6];
        unsigned char dst[6];
        unsigned char type;
    };

    DB *dbp;
    int ret;

    if ((ret = db_create(&dbp, dbenv, 0)) !=0)
        handle_error(ret);

    if ((ret = dbp->open(dbp, NULL, "pearson.db", NULL, DB_BTREE, DB_CREATE, 0600)) !=0)
        handle_error(ret);

    const DBT *pkey;
    const DBT *pdata;

    struct pearson_record p;
    DBT data, key;

    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    memset(&s, 0, sizeof(struct pearson_record));

Now the above code is written by looking at a example from the DB reference guide. but i don't understand what is const DBT. Also they have added the value inside structure using memcopy which I know is the right way, but now I want to memcopy the values passed which are mentioned above and store them in the structure pearson_record. How should I go with this?? Any kind of help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2024-12-06 23:58:09

请贴出完整的代码。你提到“他们memcopy”(我假设你指的是memcpy),但我看到的只是一堆memset(*,0)。希望你没有混淆他们。

另外,“他们使用 memcopy 添加了结构内部的值,我知道这是正确的方法”并不完全正确。这不一定是错误的,但是... char* 基本上被解释为 C 字符串。这是一个字节数组,表示必须以 null 结尾的字符(即最后一个字符必须为 0,相当于 '\0')。复制字符串的正确方法是使用 strcpy() (或 Windows 上的 strcpy_s ),不同之处在于 memcpy 速度更快并用于其他情况(例如指针\缓冲区管理)。

unsigned char* 不那么常用(至少我到现在为止从未见过它)。作为注释,请阅读有关 char、unsigned char、signed char、char[] 和 char* 的内容(并不是说它会以任何方式更改您的代码,而只是为了确保您理解这些差异)。

至于复制数据,我假设您的意思是 src、dst 和从 pearson_record 到 header 的类型,对吗?如果是这样,为了简单起见,我想建议 memcpy 但你说每个元素都作为 [i] 访问。这是否意味着 header.src 是一个包含多个 pearson_record.src 的数组,或者 header.src[i] 对应于 pearson_record.src[i] ?这对我来说有点不清楚。

char* src 和 char* *src 之间存在差异。

Please post the complete code. You mention "they memcopy" (which I assume you refer to memcpy), but all I see is a bunch of memset(*,0). Hope you're not confusing them.

Also "they have added the value inside structure using memcopy which I know is the right way" is not entirely true. It's not necessarily wrong, BUT... char* is basically interpreted as a C string. that is an array of bytes which represent characters which MUST be null terminated (that is the last character must be 0, equivalent to '\0'). The proper way to copy strings is using strcpy() (or strcpy_s on windows), the difference is memcpy is faster and used in other situations (such as pointers\buffer management).

unsigned char* is not so used (at least I never saw it till now). As a note read about char, unsigned char, signed char, char[] and char* (not that it changes your code in any way, but just to make sure you understand the differences).

As for copying data, I assume you mean src, dst and type from pearson_record to header, correct ? If so, for the sake of simplicity I wanted to suggest memcpy but you say that each element is accessed as [i]. Does that mean header.src is an array of more than one pearson_record.src or does header.src[i] correspond to pearson_record.src[i] ? This is slightly unclear to me.

There is a difference between char* src and char* *src.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文