简体   繁体   中英

“Implicit declaration” warning

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM