如何调用具有指针作为参数的函数

时间:2014-11-11 08:39:22

标签: c

我正在尝试调用函数

Int db5_disk_header(struct db5_raw_internal *rip, const unsigned. char *cp)
{
   fprintf(openfile, "these are headers a as %d b as %d c as %d", rip->a, rip->b, rip->c);
   ...
}

我如何在C中的main()中调用此函数?

1 个答案:

答案 0 :(得分:2)

了解Basic Pointer Operations

在这种情况下:

#include <stdio.h>

struct db5_raw_internal {
    int a, b, c;
};

int db5_disk_header(struct db5_raw_internal *rip)
{
    fprintf(stdout, "these are headers a as %d b as %d c as %d", rip->a, rip->b, rip->c);
    return 0;
}

int main(void)
{
    struct db5_raw_internal x = {1, 2, 3};
    db5_disk_header(&x); // pointer to x using the address-of operator (&)

    struct db5_raw_internal *y = &x;
    db5_disk_header(y); // y is already pointer (don't use &)

    return 0;
}