简体   繁体   中英

Syntax in recursive functions using pointers with struct variables. It doesn't work, and I think the main problem is the syntax

I'm doing a menu, and I'm trying to do a function that will look for an specific ID inside a struct , in case it finds an ID that matches it'll return the position of the number of the id, otherwise it'll bring back NULL . Any help is welcome, it doesn't even give me anything back, just compiling errors.

struct client {
    int id;
} cli[10]; // Let's say I already have cli.id = {1, 2, 3}

int idPosition(FILE *ptn1, int *arr, int idWanted, int i, int top);

int main() {
    int id, position;
    FILE *fp;
    fp = fopen("clients.txt","rb+"); // I should have the cli.id = {1,2,3}
    printf("ID: ");
    scanf("%i", &id); // Let's suppose I insert a 3
    position = idPosition(*fp, *cli, id, 0, 10); // I should get a 2, which would the position of the 3
}

int idPosition(FILE *ptn1, int *arr, int idWanted, int i, int top) {
    fread(&arr[i],sizeof(arr[i]),1, ptn1); // ptn1 is pointing to FILE *fp which already fp = fopen("...","rb+");
    if ((*(arr+i)).id == idWanted)
        return i;
    else if (idWanted == top)
        return NULL;
    return idPosition(ptn1, arr, idWanted, i+1, top);
}

I tried out your code and indeed it would not compile. The issue is in the parameter set for your ID position function. Even though the ID structure is only holding an integer, the function cannot reference the structure array via an integer array pointer. With that, I built a simple file that holds ID's for numbers 1 - 10 and then did a bit of tweaking to your code. Following is a copy of the tweaked code.

#include <stdio.h>
#include <stdlib.h>

struct client {
    int id;
} cli[10];

int idPosition(FILE *ptn1, struct client *arr, int idWanted, int i, int top); /* The second parameter needed to reference the structure array even though it only holds an integer */

int main(void) {
    int id, position;
    FILE *fp;

    fp = fopen("clients.txt","rb+");            // I should have the cli.id = {1,2,3}
    printf("ID: ");
    scanf("%i", &id);                           // Let's suppose I select a 3
    position = idPosition(fp, cli, id, 0, 10);  // I should get a 2, which would the position of the 3
    printf("Position: %d\n", position);
}

int idPosition(FILE *ptn1, struct client *arr, int idWanted, int i, int top) {
    fread(&arr[i],sizeof(arr[i]),1, ptn1);      // ptn1 is pointing to FILE *fp which already fp = fopen("...","rb+");
    if ((*(arr+i)).id == idWanted)
        return i;
    else
        if (idWanted > top)
            return NULL;
        else
            return idPosition(ptn1, arr, idWanted, i+1, top);
}

The key bits to point out is the parameter list for the "idPosition" function. Instead of "int *arr" the definition for that second parameter is "struct client *arr". That aligns with passing the "cli" array when the function is initially called.

When I tested this out, here were my results on my terminal.

@Una:~/C_Programs/Console/Recursion/bin/Release$ hexdump clients.txt 
0000000 0001 0000 0002 0000 0003 0000 0004 0000
0000010 0005 0000 0006 0000 0007 0000 0008 0000
0000020 0009 0000 000a 0000                    
0000028
@Una:~/C_Programs/Console/Recursion/bin/Release$ ./Recursion 
ID: 3
Position: 2

The first command was just to present the binary data in the "client.txt" file. Then, the call was made to the program illustrating that it was functioning in the spirit of your code design.

The takeaway from this is be mindful of passing the correct type of parameter in your functions, especially when using structures or structure arrays.

Hope that helps.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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