C 指针变化无赋值

标签 c pointers raspberry-pi segmentation-fault 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 之前改变而没有分配导致 SIGSEGVget_fingerprint_enroll 的程序中。 fingerprint_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;

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

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

我只在fingerprint_init中初始化了passwordaddressfd。我做错了什么?

最佳答案

在您的 scanf("%u", &id); 中,iduint8_t [通常表示为 unsigned char], IMO,你应该写scanf("%c", &id);

%u 表示 unsigned int

同样适用于 printf("Enrolling ID #%u\n", id);

关于C 指针变化无赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199828/

相关文章:

c - 通过知道线程名称来设置线程的优先级

php - 使用 PHP shell_exec 在后台运行 python 脚本

c - 正确的返回语句和函数类型

c - printf保存keepalive定时器

c++ - 由于从客户端启动守护程序应用程序,服务器端口无法返回监听状态并处于 close_wait 状态

c++ - 数组指针的增量

pointers - 指针的 slice ,当传递给对象时,得到具有其他地址的指针

c++ - -> 的基操作数没有非指针类型的指针 ’

arduino - 如何使用 Nest 设置当前温度

java - 将文件从树莓派发送到智能手机蓝牙