"Implicit declaration" warning - c

For this code:
int i=0; char **mainp;
for(i=0;i<2;++i)
{
mainp[i]=malloc(sizeof(char)*200);
if(!scanf("%[^#],#",mainp[i]))
break;
if(i<2)
scanf("%[^#],#",mainp[i]);
}
GCC emits the warnings:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
warning: ‘mainp’ may be used uninitialized in this function
And I get a segmentation fault at runtime
input:(P>Q),(Q>R),-R#-P
output:
(P>Q),(Q>R),-R
(empt slot)
i expected to give me
(P>Q),(Q>R),-R
-P //where should i fix in my code such that it gives me expected
//output

Problem #1:
warning: ‘mainp’ may be used uninitialized in this function
You need to allocate memory for the array of arrays first.
char **mainp = malloc(sizeof(char*)*2);
Problem #2:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
You need to include stdio.h at the top of your file:
#include <stdio.h>
Problem #3: (Not included in your compiling warnings)
Remember to free both the allocated array members and also the array of array address.

gcc expects this line at the beginning of your file:
#include <stdio.h>
and a declaration of mainp like this one:
char *mainp[2];

You shouldn't use functions without declaring them; you used scanf, but at no point in your code is scanf declared. Since it's a standard library function it's declared in one of the standard headers, stdio.h, so you just need to include it:
#include <stdio.h>
Brian's answer is good for the other part

Related

Apparently erroneous implicit declaration of function warning

I am getting a warning for lstat being implicitly declared, but it should be explicitly declared in the included header.
Compiling the following:
// Standard library
#include <stdio.h>
// Feature test macro
#define _POSIX_C_SOURCE 200809L
// POSIX inclusions
#include <sys/stat.h>
// Function
int main()
{
struct stat x;
char* s = "/data/file";
char* t = "/data/link";
lstat(s,&x);
printf( "%i\n", S_ISLNK(x.st_mode) );
lstat(t,&x);
printf( "%i\n", S_ISLNK(x.st_mode) );
return 0;
}
With the command:
clang -std=c11 test.c
Produces:
test.c:16:4: warning: implicit declaration of function 'lstat' is invalid in C99 [-Wimplicit-function-declaration]
lstat(s,&x);
^
1 warning generated.
Running the output with a regular file at "/data/file" and a link at "/data/link" gives the following output:
0
1
So lstat is obviously being seen by the compiler and used.
If I remove the "-std=c11" option, there is no warning. I get essentially the same thing in gcc, no warning with no option, similar warning with the option.
I am sure I am missing something since both gcc and clang do the same thing, so:
How can I be implicitly defining a function but still accessing the full definition in sys/stat.h?
What can I do to not get this warning?
Feature test macros should be defined before any header is included, including the standard C ones.
So move
#define _POSIX_C_SOURCE 200809L
at the beginning of your file.
If you misplace the feature test macro, the function may not be declared in the header at all. In C89 it is allowed to call an undeclared function. It will be implicitly declared. That is not allowed since C99, but the compilers are choosing to just give a warning about it instead of terminating compilation, and continue to declare the function implicitly.
Without a prototype default argument promotion will be done on the call arguments to the implicitly-declared function, which for the pointers you pass means that they are passed without any conversion. So the arguments you are giving happen to coincide with the actual parameter types of lstat. So there is no problem, but this is obviously dangerous in general, so don't rely on the implicit declaration.

C - Cannot take input after allocating dynamic array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need someone to tell me what I am doing wrong over here.
void main(void) {
int *arr,n,i;
printf("Enter the number of elements you want to enter=");
scanf("%d",&n);
arr = (int*)calloc(n,sizeof(int));
if(!arr)
return;
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
free(arr);
}
This is nothing but a simple program to input the number of items in an array then take the input and print the inputted values. But whenever I go to input the values in the array after taking the number of items from the user, my program CRASHES. I don't understand what I am doing wrong over here. Need some help immediately. Please run the program and see if it also crashes on other systems. I executed the program using an online compiler and strangely there it was running fine. I am using the Visual Studio 2013 command line compiler to execute my program.
Compiling this piece of code with gcc I get:
calloc.c: In function ‘main’:
calloc.c:4:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
calloc.c:5:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
calloc.c:7:17: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default]
calloc.c:18:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
using clang:
calloc.c:1:1: error: 'main' must return 'int'
void main(void) {
^
calloc.c:4:5: warning: implicitly declaring C library function 'printf' with type 'int (const char *, ...)'
printf("Enter the number of elements you want to enter=");
^
calloc.c:4:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
calloc.c:5:5: warning: implicitly declaring C library function 'scanf' with type 'int (const char *, ...)'
scanf("%d",&n);
^
calloc.c:5:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'scanf'
calloc.c:7:17: warning: implicitly declaring C library function 'calloc' with type 'void *(unsigned long, unsigned long)'
arr = (int*)calloc(n,sizeof(int));
^
calloc.c:7:17: note: please include the header <stdlib.h> or explicitly provide a declaration for 'calloc'
calloc.c:18:5: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration]
free(arr);
^
4 warnings and 1 error generated.
As clang kindly pointed out, you need to add
#include <stdlib.h>
#include <stdio.h>
to the top of your code, as you are using functions that are declared within them. The functions you are using are in the standard C library, so the code will still compile but you should expect the unexpected. The reason your program crashes is likely to be that the compiler has made an incorrect assumption about the return type of calloc, as it doesn't have the correct declaration. Many systems will let you get away with this but at the very least they should warn you.
On a side note, please see this discussion on casting the return value of malloc (don't cast it). This advice also applies to calloc.
edit
Here's a minimal example which might shed some light on the issue:
a.c
#include <stdio.h>
int main()
{
printf("%f\n", f());
return 0;
}
b.c
float f()
{
return 4.2;
}
These two files can be compiled separately into object files:
gcc -c a.c # creates a.o
gcc -c b.c # creates b.o
No warnings or errors are generated. Switching to C99 mode causes an "implicit function declaration" warning. Note that at compile-time, the function definition isn't needed. These two object files can be linked:
gcc a.o b.o
Now, the linker finds the definitions of the functions so there is no error but it is already too late. The implicit declaration of f() when a.c was compiled causes the output to be incorrect when the program is run (I get 0.000000).
The same thing has happened here as in your question. The function calloc can be found, so there is no error at link-time. However, an incorrect assumption is made about the type that it returns, so it doesn't behave as expected on your system.

c - warning: implicit declaration of function ‘printf’

I know alot of similar questions were asked before but i couldn't find something that would fix this warning i get:
MyIntFunctions.c:19:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
Occurs here:
void IntPrint (const void *key)
{
printf("%d", *(int*)key); // line 19
printf("\t-->\t");
}
and a similar warning:
MyStringFunctions.c:22:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
void StringPrint (const void *key)
{
printf("%s",(char*)key); //line 22
printf("\t-->\t");
}
I really want to understand what is wrong so i won't do that again in the future.
You need to include the appropriate header
#include <stdio.h>
If you're not sure which header a standard function is defined in, the function's man page will state this.
You need to include a declaration of the printf() function.
#include <stdio.h>
the warning or error of kind IMPLICIT DECLARATION is that the compiler is expecting a Function Declaration/Prototype..
It might either be a header file or your own function Declaration..
warning: incompatible implicit declaration of built-in function 'printf'
warning: incompatible implicit declaration of built-in function 'scanf'
the above warnings of compiler says that there is need to include declaration of printf and scanf i.e. include appropriate header
#include <stdio.h>

warning: implicit declaration of function

I'm programming in C and my gcc compiler gives me the following warning in my function call in mySedondFile.c:
implicit declaration of function 'func'
The function prototype is declared in myfile.h as:
void func(char*);
Function definition is in myfile.c
void func(char*x);
mySecondFile.c contains:
#include "myfile.h"
func("Hello");
I'm missing why this would complain.
That error is emitted because func has not been declared at the point at which you call it.
It sounds like your header files aren't quite as you describe. Perhaps there is some conditional code. Maybe you have a header guard that is not working right. Another possibility is that you have got a letter case error and declared the function Func but called it with func. Very hard to say without seeing the actual files but you need to look for a reason why func is not declared in the mySecondFile.c translation unit.
To illustrate this a little more clearly, the following code:
int main(void)
{
func("Hello");
return 0;
}
results in this warning:
prog.c: In function ‘main’:
prog.c:3: warning: implicit declaration of function ‘func’
which is exactly as you report.
According to your description, your code includes a header file which declares func. The compiler begs to differ with you and it remains for you to work out why func is not declared.

Incompatible implicit declaration of built-in function ‘malloc’

I'm getting this error:
warning: incompatible implicit declaration of built-in function ‘malloc’
I am trying to do this:
fileinfo_list* tempList = malloc(sizeof(fileinfo_list));
Just for the reference the struct used at hand is:
typedef struct {
fileinfo** filedata;
size_t nFiles;
size_t size;
size_t fileblock;
} fileinfo_list;
I don't see anything wrong with what I've done. I'm just creating a tempList with the size of 1 x fileinfo_list.
You likely forgot to #include <stdlib.h>
You need to #include <stdlib.h>. Otherwise it's defined as int malloc() which is incompatible with the built-in type void *malloc(size_t).
You're missing #include <stdlib.h>.
The stdlib.h file contains the header information or prototype of the malloc, calloc, realloc and free functions.
So to avoid this warning in ANSI C, you should include the stdlib header file.
The only solution for such warnings is to include stdlib.h in the program.

Resources