C指针更改而没有赋值

时间:2014-11-29 07:06:19

标签: c pointers raspberry-pi sigsegv fingerprint

我正在尝试访问raspberry pi中的指纹识别器。我写了一个基于"Adafruit Fingerprint Sensor Library"的库。这是我的示例代码:

int main(int argc, char **argv)
{
    int res = wiringPiSetup();
    if (res == -1)
    {
        perror("wiringPiSetup");
        return EXIT_FAILURE;
    }

    /* malloc fingerprint_t */
    fingerprint_t *fp = fingerprint_init(argv[1], atoi(argv[2]));

    /* this call to fingerprint sensor works fine */
    if (fingerprint_verify_password(fp))
    {
        printf("Found fingerprint sensor\n");
    }
    else
    {
        printf("Did not find fingerprint sensor\n");
        return EXIT_FAILURE;
    }

    /* fp pointer is 0x12008 here */

    while(true)
    {
        uint8_t id;
        printf("type in the ID # you want to save finger print:");
        scanf("%u", &id);
        printf("Enrolling ID #%u\n", id);

        /* same fp pointer is 0x10000 here */

        while (true)
        {
            /* Program received signal SIGSEGV, Segmentation fault inside this function */
            uint8_t res = get_fingerprint_enroll(fp, id);
            if (!res)
            {
                printf("*****************\n");
                break;
            }
        }
    }
}

fp指针在第一个while循环之前就可以了,但它会在第二个while之前发生变化,而不会在SIGSEGV的程序中导致get_fingerprint_enrollfingerprint_t结构是这样的:

typedef struct _fingerprint_t
{
    uint16_t finger_id;
    uint16_t confidence;
    uint16_t template_count;
    uint32_t password;
    uint32_t address;
    uint32_t fd;
} fingerprint_t;

我在fp中为fingerprint_init分配内存,如下所示:

fingerprint_t *fp = (fingerprint_t *) malloc(sizeof(fingerprint_t));

我只在password内初始化addressfdfingerprint_init。我做错了什么?

1 个答案:

答案 0 :(得分:4)

scanf("%u", &id);中,由于id uint8_t [通常表示为unsigned char],IMO,您应该写scanf(" %c", &id);

%u表示unsigned int

printf("Enrolling ID #%u\n", id);

也是如此