A type without a reference used to declare a function with a binding

I am trying to write a function that takes as a parameter a pointer to a type I created with a typedef called NodeType . I vaguely understand that typedef names have no binding. I'm not sure why I get the following error if both instances of type NodeType end up in the same translation unit.

Here is the code:

 #include <stdio.h> int main(){ typedef struct NodeTag{ char* Airport; NodeTag * Link; } NodeType; //Declare print function void printList(NodeType *); void printList(NodeType * L){ //set N to point to the first element of L NodeType * N = L; //if the list is empty we want it to print () printf("( "); //while we are not at the Link member of the last NodeType while(N != NULL){ //get the Airport value printed printf("%s", N->Airport); //advance N N= N->Link; if(N != NULL){ printf(", "); } else{ //do nothing } } printf(")"); } return 0; } 

This is the error I encountered:

 linkedlists.c: In function 'int main()': linkedlists.c: error: type 'NodeType {aka main()::NodeTag} with no linkage used to declare function 'void printList(NodeType*) with linkage [-fpermissive] 

Thank you for your help!

0
source share
2 answers

Your printList function printList defined in the body of main , which confuses the compiler. Move printList outside the main body as follows:

 #include <stdio.h> typedef struct NodeTag{ char* Airport; NodeTag * Link; } NodeType; //Declare print function void printList(NodeType *); int main(){ return 0; } void printList(NodeType * L){ //set N to point to the first element of L NodeType * N = L; //if the list is empty we want it to print () printf("( "); //while we are not at the Link member of the last NodeType while(N != NULL){ //get the Airport value printed printf("%s", N->Airport); //advance N N= N->Link; if(N != NULL){ printf(", "); } else{ //do nothing } } printf(")"); } 

After you have done this and ask him to compile, you need to figure out how and where to call printList from main .

0
source

You cannot declare your function inside the main function. Place the function prototype and declaration outside the main loop. The prototype of the function ( void printList(NodeType *); ) must be declared before the function is actually used. Also declare your structure outside the core and in front of your function.

You also have an error in your typedef

  typedef struct NodeTag{ char* Airport; NodeTag * Link; <-- missing struct prefix } NodeType; 
0
source

Source: https://habr.com/ru/post/1487559/


All Articles