简体   繁体   English

在C中作为函数参数的指针为NULL

[英]Pointer as function parameter in C is NULL

Pardon any ignorance - I'm relatively new to C. I have the below function: 原谅任何无知-我对C还是比较陌生。我具有以下功能:

int get_fingerprint_device(struct fp_dev *device) {
    struct fp_dscv_dev **devices;
    int rtn;

    if (!(devices = fp_discover_devs())) {
        rtn = -1;
    }
    else {
        if (!(device = fp_dev_open(*devices))) {
            rtn = -2;
        }
        else {
            if (device) {
                printf("Device OK\n");
            }

            rtn = 1;
        }

        fp_dscv_devs_free(devices);
    }

    return rtn;
}

And I have a main() as follows: 我有一个main()如下:

int main(void)
{
    // Vars
    struct fp_dev *device;
    struct fp_print_data **print_data;
    struct fp_img **img;
    int err;

    // Init libfprint
    fp_init();

    // Get the first fingerprint device
    if ((err = get_fingerprint_device(device)) < 0) { // Errorz
        if (err == -1) {
            error("No devices found");
        }
        else if (err == -2) {
            error("Couldn't open the device");
        }

        return 1;
    }

    if (device == NULL) {
        printf("No\n");
        return 1;
    }
    else {
        printf("Yes\n");
        return 0;
    }

    fp_enroll_finger_img(device, print_data, img);

    // Deinit libfprint
    fp_exit();

    return 0;
}

The result when run is that it first prints "Device OK" and then prints "No". 运行时的结果是,它首先打印“设备正常”,然后打印“否”。

Why is it that device is a valid pointer in get_fingerprint_device() but not when back in main()? 为什么设备在get_fingerprint_device()中是有效的指针,但在main()中返回时却无效? My understanding is that I should be using a double pointer here such that the inner pointer value can change and be propagated back to main()... however the implementation has so far eluded me. 我的理解是,我应该在此处使用双指针,以便内部指针的值可以更改并传播回main()...但是,到目前为止,实现尚无法理解。

This is because device is passed into get_fingerprint_device by value. 这是因为device通过值传递到get_fingerprint_device In other words, the function gets its own copy of device pointer. 换句话说,该函数获得其自己的device指针副本。 Then it assigns something to it, but the change is not visible outside that function, so once it returns, the caller (in this case main ) does not see any change. 然后,它为其分配了一些内容,但是该更改在该函数外部不可见,因此一旦返回,调用者(在本例中为main )将看不到任何更改。 If you want this to work, you have to pass pointer to pointer into get_fingerprint_device function (ie struct fp_dev **device ). 如果您希望这样做,则必须将指向指针的指针传递给get_fingerprint_device函数(即struct fp_dev **device )。

You're a little confused about passing pointers in C - currently you're just passing "wild" (unallocated) pointers. 您对在C中传递指针有些困惑-当前,您只是传递“野生”(未分配)指针。 You should change: 您应该更改:

struct fp_dev *device;

to: 至:

struct fp_dev device;

and change: 并更改:

if ((err = get_fingerprint_device(device)) < 0)

to: 至:

if ((err = get_fingerprint_device(&device)) < 0)

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

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