C pointer changes without assignment

115 Views Asked by At

I'm trying to access a fingerprint reader in raspberry pi. I have written a library based on "Adafruit Fingerprint Sensor Library". Here is my sample code:

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 pointer is OK before first while loop but it changes before second while without assignment which cause SIGSEGV in the program at get_fingerprint_enroll. fingerprint_t struct is like this:

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;

I allocate memory for fp in fingerprint_init like this:

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

I only initialize password, address and fd inside fingerprint_init. What I'm doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

In your scanf("%u", &id);, as id is uint8_t [which is usually represented as unsigned char], IMO, you should write scanf(" %c", &id);.

%u denotes unsigned int.

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