Home > front end >  Unsure of "return type defaults to 'int', and undefined references to ‘main’ and `rea
Unsure of "return type defaults to 'int', and undefined references to ‘main’ and `rea

Time:06-16

So I'm not into coding C at all, for a job I'm interviewing for (not a programmer role) they'd like me to fix this code. I did tell them I couldn't code in C but they insisted that I should be able to find the answer with some research. I've spent a few hours reading various articles and looking at the errors a compiler throws up. But in all honesty I don't understand 90% of contents of these articles or what the errors even really mean.

So far the answer I have is this: "Linker errors, Undefined references to ‘main’ and `read_from_net'. Implicit declaration of function error." But I'm not sure if that's just because it's just an extract of a larger piece of code that I'm missing required parts of.

f() {
        char buf[80], *p = buf;
        while ((*p = read_from_net()) != '\0') {
        }
    }   

Any help is appreciated, as I'm not really sure I can learn to code in C in a few days, enough to see the errors.

Many thanks

CodePudding user response:

1. f() {
2.    char buf[80], *p = buf;
3.    while ((*p = read_from_net()) != '\0') {
4.    }
5. }

with output from one of the common compilers:

source>:1:1: error: return type defaults to 'int' [-Wimplicit-int]
    1 | f() {
      | ^
<source>: In function 'f':
<source>:3:18: error: implicit declaration of function 'read_from_net' [-Wimplicit-function-declaration]
    3 |     while ((*p = read_from_net()) != '\0') {
      |                  ^~~~~~~~~~~~~
<source>:5:1: warning: control reaches end of non-void function [-Wreturn-type]
    5 | }
    1. In C every function declaration must include the type of the value the function will return. If the function doesn't return a value at all, it should be declared (to return) void. f() lacks this information and according to ancient rules this would mean that the declaration defaulted to returning an int, an integer. This implicit declaration is not required to work anymore according to the C standard (since C99 I believe).
    1. A call is made to a function for which no declaration has been seen by the compiler. In earlier days, this would slip through as an implicit function declaration and the compiler would assume that the function return int (the default again). This does however not match the type the return value is assigned to. p is a char* which makes dereferencing p (*p) a char - so, the declaration char read_from_net(); should be found somewhere and an #include <file_that_declares_read_from_net.h> should be added to the top of the file.
    1. Since the compiler chose int to be the return type for f() in 1., f() should return and int - but it doesn't. Control (the "executor of the program" if you will) leaves the scope of the function without returning anything. We can now go back to 1. and realize that f() should have been void f() to fix that issue.

"Linker errors, Undefined references to ‘main’ and `read_from_net'.

  • main - This is the start of every standard C program. It's a function with the declaration int main(void); or int main(int, char**); (with some platform specfic extras). Without this function a compiled program wouldn't know which function to call first. Now, you can have C files without main. It's just that when all the C files that makes up a program are compiled and linked together, one (and only one) of them must have this main function.
  • read_from_net - It seems that you've missed to link with the library that defines the function read_from_net. So, not only do you need #include <file_that_declares_char_read_from_net.h>, you also need to link your final executable (the program you'll finally run) with a library or object file that contains this function.
#include <file_that_declares_read_from_net.h>

void f() {
    char buf[80], *p = buf;
    while ((*p = read_from_net()) != '\0') {
          p; // some progress of p is probably wanted
        // note: p may go out of bounds if `\0` is not found within 80 chars
    }
}

int main() {
     // does something and probably calls f()
}

and compilation could look like this:

compiler -o program.exe your_program.c file_that_defines_read_from_net.o

CodePudding user response:

Some fixes explained with comments below

int f(void)                                     // Function f takes no parameters (void), and returns an integer
{
    char buf[80], *p = buf;
    while ((*p   = read_fron_net()) != '\0') {  // Increment p every time a character is read by using p  
    }

    return p-buff;                              // Return the number of characters read.
}  
  •  Tags:  
  • c
  • Related