martiwg
martiwg

Reputation: 95

Pointers + structs + functions in C identifier undefined

So this is my code and I don't understand why I get that identifier "pers" is undefined, when I'm clearly pointing at it from another function, which is as far as I know, the utility of pointers.

I've gone through some research but nothing seemed to solve my issue since I'm dealing with structs and all that.

Also one of the requirements is that tle so called "leer_persona();" cant have any value in the parenthesis

#include <stdio.h>

typedef struct{
    int num;
    char letra;
}tdni;
typedef struct{
    char nom[20];
    tdni dni;
}tpersona;

tpersona leer_persona();
void mostrar_persona(tpersona p);

int main(){
    tpersona pers;
    pers = leer_persona();
    mostrar_persona(pers);

    return 0;
}

tpersona leer_persona(){
    int i=0;
    int *fp;
    fp = &pers;

Thanks.

Upvotes: 1

Views: 112

Answers (1)

paulsm4
paulsm4

Reputation: 121649

Pers has function scope in "main()". It is not visible outside of "main()".

https://www.geeksforgeeks.org/scope-rules-in-c/

Function scope begins at the opening of the function and ends with the closing of it.

See this link for more details: C - Scope Rules

If you want to use "pers" in another function, you'd typically pass it as a function parameter, e.g. tpersona leer_persona(tpersona * pers). In this example, I passed parameter "pers" by reference, instead of copying by value.

Upvotes: 1

Related Questions