gpt4 book ai didi

C 指针变化无赋值

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:33 25 4
gpt4 key购买 nike

我正在尝试访问 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com