Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

error
incompatible
declaration
built-in
function

"incompatible implicit declaration of built-in function" resolved

Educative Answers Team

Problem

Take a look at the simple C code below. The code tries to dynamically allocate memory to an array of x,yx,y coordinates using malloc (defined in a user-defined struct):

#include<stdio.h>

typedef struct {
    int x;
    int y;
} coordinate;


int main() {

    int num_of_coordinates = 5;
    coordinate* arr = malloc(sizeof(coordinate)*5);
    
}

Executing the code above results in the following error at main.c:12:23:

incompatible implicit declaration of built-in function ‘malloc’

The error is self-explanatory; the compiler could not find a definition of the malloc function that matches the definition it is being used in. The compiler may have found some other definition that has a different return type or parameters than the way we used it, or it may not have found one at all.

A function needs to follow the exact definition to which it was declared; in this case, the compiler could not find any such corresponding definition and, thus, output an error.

Solution

The solution is simple; the file that contains the correct definition of the function being used needs to be included in the working file so that the compiler can locate it. In this case, its <stdlib.h>.

After inclusion of the particular file, see how the code compiles without any errors:

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

typedef struct {
    int x;
    int y;
} coordinate;


int main() {

    int num_of_coordinates = 5;
    coordinate* arr = malloc(sizeof(coordinate)*5);

}

This error is not just specific to the malloc function; it can occur while using any built-in function. However, the method of resolving this error remains the same.

RELATED TAGS

error
incompatible
declaration
built-in
function
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring