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 issues 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 am getting segmentation error at runtime

input: (p> Q) (Q> R) - R # -P output: (p> Q) (Q> R) - P (empt slot)

I had to give me (p> Q) (Q> R) - P -P // where should I fix in my code so that it expects me // exit

+2


a source to share


3 answers


Problem # 1:

warning: 'mainp can be used uninitialized in this function

First, you need to allocate memory for an array of arrays.

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 beginning of the file:

#include <stdio.h>

      

Problem # 3: (Not included in your compilation warnings)

Don't forget to free both the allocated array elements and the array array.

+10


a source


gcc expects this line at the beginning of your file:

#include <stdio.h>

      



and a mainp declaration like this:

char *mainp[2];

      

+1


a source


You shouldn't use functions without declaring them; you used scanf

but by no means was your code declared scanf

. Since it is a standard library function 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

0


a source







All Articles