warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration] - c

I am new to C programming which's why I am confused with its syntax.
Question: A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as
each of the nine 3 × 3 subgrids, must contain all of the digits 1 ⋅ ⋅ ⋅ 9. Figure
4.26 presents an example of a valid Sudoku puzzle. This project consists of
designing a multithreaded application that determines whether the solution
to a Sudoku puzzle is valid.
There are several different ways of multithreading this application. The one
suggested strategy is to create threads that check the following criteria:
• A thread to check that each column contains the digits 1 through 9
• A thread to check that each row contains the digits 1 through 9
#include <stdlib.h>
int i,j,m,b,k;
void main()
{
int a[9][9]={1,2,3,4,5,6,7,8,9,
4,5,6,7,8,9,1,2,3,
7,8,9,1,2,3,4,5,6,
2,3,4,5,6,7,8,9,1,
5,6,7,8,9,1,2,3,4,
8,9,1,2,3,4,5,6,7,
3,4,5,6,7,8,9,1,2,
6,7,8,9,1,2,3,4,5,
9,1,2,3,4,5,6,7,8};
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
{
printf("Success");
}
else{
printf("Failed");
}
}
int rowcheck(int a[9][9])
{
int c[10]={0};
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
c[a[i][j]]++;
}
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d row \n",k,c[k],i+1);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
return 1;
}
int colcheck(int a[9][9])
{
int c[10]={0};
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
c[a[i][j]]++;
}
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d column \n",k,c[k],i+1);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
return 1;
}
int cubecheck(int a[9][9])
{
int c[10]={0},count=0;
for(m=0;m<9;m+=3)
{
for(b=0;b<9;b+=3)
{
for(i=m;i<m+3;i++)
{
for(j=b;j<b+3;j++)
{
c[a[i][j]]++;
}
}
count++;
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d box\n",k,c[k],count);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
}
return 1;
}```
I am getting this error plz help.
```proj1.c: In function ‘main’:
proj1.c:18:8: warning: implicit declaration of function ‘rowcheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~
proj1.c:18:26: warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~
proj1.c:18:43: warning: implicit declaration of function ‘cubecheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~~
proj1.c: At top level:
proj1.c:136:1: error: expected identifier or ‘(’ before ‘}’ token
}```
^

You need to give declarations for your functions before main() method since you define them afterwards:. A forward function declaration just tells the compiler that, somewhere in your code, a function with the same name will be defined, in this case after its usage in main().
Usually, in C/C++ you define function prototypes in header files, then include them in your main source code, so the functions can safely be defined after they are called.
#include <stdio.h>
#include <stdlib.h>
// add forward declarations for your functions here
int rowcheck(int [][9]);
int colcheck(int [][9]);
int cubecheck(int [][9]);
int main() {
// your code
return 0:
}
// method definitions afterwards
int rowcheck(int a[9][9]) {
// your definition here
}
int colcheck(int a[9][9]) {
// your definition here
}
int cubecheck(int a[9][9]) {
// your definition here
}
Also, define your main method as int main() rather than void main().

Related

How would I get this code to be executed without using stdlib.h

I keep receiving an error regarding malloc and I'm trying to find out how to get this code to work without using stdlib.h in the header. Just stdio.h, is this possible and how? As I'm totally confused
#include <stdio.h>
void allocate(int* score_array, const int input)
{
int iter;
for(iter = 1;iter <= 11;++iter)
{
if( (input < iter*10) && (input >= (iter-1)*10 ) )
{
++(score_array[iter-1]);
}
}
}
void printf_star(const int len)
{
int iter;
for(iter = 0;iter < len;++iter)
{
printf("*");
}
printf("\n");
}
int main()
{
int iter, size, temp;
int* buffer;
int score_array[11];
for(iter = 0;iter < 11;++iter)
{
score_array[iter] = 0;
}
printf("How many grades will you be entering?\n");
printf("Enter a number between 1 and 100: ");
scanf("%d", &size);
buffer = (int*)malloc(size*sizeof(int));
for(iter = 1;iter <= size;++iter )
{
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
if( (temp>=0) && (temp <= 100) )
{
buffer[iter-1] = temp;
}
else
{
do
{
printf("Invalid Value!\n");
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
}
while( (temp < 0) || (temp > 100) );
}
}
for(iter = 1;iter <= size;++iter)
{
allocate(score_array, buffer[iter-1]);
}
for(iter = 0;iter < 11;++iter)
{
printf_star(score_array[iter]);
}
return 0;
}
I keep getting this error:
hw08.c: In function ‘main’:
hw08.c:56: warning: incompatible implicit declaration of built-in function ‘malloc’
This is only a warning, not an actual error, so the program still compiles.
To eliminate the warning you can declare the malloc in your file:
#include <stdio.h>
extern void * malloc(unsigned long);
You could also just include stdlib.h, unless you have a major reason not to.
Header files just define the functions prototypes by using the extern keyword. The actual implementation of malloc resides in libc depending on the OS.
Not defining a function/system call prototype is indeed a warning, not a compile-time error, contrary to what many have conveyed in the comments!
Coming to the actual workaround, if you want to avoid using the #include <stdlib.h>, you either need to use:
#include <malloc.h> (deprecated since c89)
Define the header all by yourself, with extern void * malloc(size_t);
Credits to #Chris Rouffer too! :)
You need to include stdlib.h if you want to access the malloc() function, because that is where it is defined. Otherwise the compiler doesn't know what to do.
You really are supposed to include the header in your code if you want to use the function, however, in theory you could just paste the implementation of malloc() in your source and then use it from there without the header. This is a bad idea however, since anybody looking at the code would expect malloc() to refer to the standard implementation defined in stdlib.h.

Warning: type of ‘numRest’ defaults to ‘int’ (in function 'sleep')

I get a warning In function ‘sleep’: warning: type of ‘numRest’ defaults to ‘int’ and I have no idea why. It runs perfectly fine but apparently I got this warning. Does anyone else get this warning when they run it?
void sleep(numRest){
if ((numRest >= 0) && (numRest <=4)){
printf("Sleep deprived!");
}
else if ((numRest > 4) && (numRest < 6)){
printf("You need more sleep.");
}
else if ((numRest >= 6) && (numRest < 8)){
printf("Not quite enough.");
}
else{
printf("Well done!");
}
return;
}
int main()
{
int numSleep = -1;
if (numSleep == -1){
printf("Test 1\n");
printf("Input: -1\n");
printf("Expected Result: Error, you cannot have a negative number of hours of sleep.\n");
printf("Actual Result: ");
sleep(numSleep);
printf("\n\n");
numSleep = 4.5;
printf("Test 2\n");
printf("Input: 4.5\n");
printf("Expected Result: You need more sleep.\n");
printf("Actual Result: ");
sleep(numSleep);
printf("\n\n");
}
return 0;
}
The issue is with the function signature definition.
void sleep(numRest) {
should be
void sleep(int numRest) {
Otherwise, the compiler will "assume" (now obsolete by latest standard) that the missing datatype is int.
Related, quoting from C11, Major changes (over previous versions) list
remove implicit int
That said,
sleep() is a library function already, prototyped in unistd.h, do not try to use the same for for user-defined functions.
int main() should be int main(void), at least for hosted environments to conform to the standard.
You have to explicitly put variable type in function declaration as:
void sleep(int numRest) {
//your code here
}

error C2447: '{' : missing function header -- Can't resolve this error, what's wrong?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "bcio2.h"
int error, x;
char totalimpulse[80], averageimpulse[80];
void validate_number();
int main(void)
{
clrscr();
do{
printf("\nTotal Impulse delivered: ");
gets(totalimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
{ //error C2447
clrscr();
do{
printf("\nAverage Impulse delivered: ");
gets(averageimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
The brackets seen to match and there doesn't seem to be any unnecessary semicolons. I'm assuming this is the correct way to display input/validation. It works fine when run with just the do…while(); loop for totalimpulse but when I copy/paste the exact same between another pair of { } I get just that C2447 error.
The code that starts where the error is is not inside main, or any other function for that matter. If you remove the braces on the error line and the one preceding it, then your second loop will also beside of main. If you want that section to be a different function, you have to include the header for that function. What you put at the top for validate_number is just a promise that you will define that function somewhere (although if you mean for that section at the bottom to be validate_number, I'm pretty sure you don't want it to be recursive).
Right now you just have a block of code, outside any function.
I'm assuming from the rest of your code, that this block of code is supposed to be the definition of void validate_number();, like this:
void validate_number()
{
clrscr();
do{
// ...
return 0;
}
Do note that a void function cannot return a value, so your return 0 should be removed.

Implementing AI pointer error

For my uni project I'm trying to create a basic tank game in C, but I've only just started learning C and have a very basic understanding of C. So I've started working on some simple code for a AI player, but when I compile it with GNU GCC compiler it comes up with these errors and I've got no clue how to proceed. So help would be great please! :D
Line 41 warning: passing argument 3 of 'AIMove' makes pointer from integer without a cast [enabled by default]
Line 19 note: expected 'int (*)()' but argument is of type 'int'
int PosCheck(int T1Pos, int T2Pos)
{
int a;
a = T2Pos - T1Pos;
if(a == 0) // Stop the tanks trying to overlay
{
return 0;
}
if(a >= 1 || a < 0) // Allows the tanks to move foward
{
return 1;
}
}
int AIMove(int T1Pos, int T2Pos, int PosCheck()) // AI movement
{
int b, c;
if(PosCheck(T1Pos, T2Pos) == 0) // Choose retreat options or stands still
{
b = 3 + round(3*(int)rand()/(int)RAND_MAX);
return b;
}
if(PosCheck(T1Pos, T2Pos) == 1) // Chooses foward options
{
c = 1 + round(3*(int)rand()/(int)RAND_MAX);;
return c;
}
}
main()
{
int T1Pos;
int T2Pos;
int T2MC;
T2MC = AIMove(T1Pos, T2Pos, PosCheck(T1Pos, T2Pos));
}
This function takes another function as a parameter because of these parens:
int AIMove(int T1Pos, int T2Pos, int PosCheck()) // AI movement
^^
But when you call it, you are passing the result of a same-named function:
T2MC = AIMove(T1Pos, T2Pos, PosCheck(T1Pos, T2Pos));
What is the PosCheck parameter supposed to do? Inside AIMove you call it, but it's not clear if you want the global PosCheck function or the argument.
By the way, the usual way to declare a function pointer is with an asterisk:
int AIMove(int T1Pos, int T2Pos, int (*PosCheck)()) // Obviously a pointer.
If there's nothing in particular you are trying to accomplish there, just delete the parameter and the argument.
T2MC = AIMove(T1Pos, T2Pos);
int AIMove(int T1Pos, int T2Pos)

Main is usually a function?

I do this question because i was doing a basic program, and i have a warning when i compilate it, it says "warning: 'main' is usually a function"" and then make a error of syntaxis in the same line.
My program is about a palindrome, in spanish we say "capicua".
Thanks for the help.
I program in C.
int T=10;
int CargarVector(char Vec[T]);
int escapicua(char Vec[T])
int main()
{ //here is the error!!
char Vec[T];
cargarVector(Vec);
escapicua(Vec);
return 0;
}
int CargarVector(int Vec[T])
{
int i=0;
printf("ingrese letra");
aux=getche();
while(aux!='.'&&i<T)
{
while(aux<'a'||aux>'Z')
{
printf("Error, ingrese letra del Abcdario")
aux=getche();
}
Vec[i]=aux;
i++;
printf("ingrese letra");
aux=getche();
}
r=i;
return 0;
}
int escapicua(char Vec[T])
{
int i,c;
for(i=0;i<(T/2),i++)
{
if(Vec[i]!=Vec[(T-1)]
{
C++
}
if(C>0)
{
printf("No es capicua");
}
else
{
printf("Es capicua")
}
}
return 0;
}
This is because you forgot to put a semicolon after the forward declaration on the previous line:
int escapicua(char Vec[T]);
// Here ------- ^
Note that array size and parameter names are ignored in function declarations, so the declaration below would be equivalent:
int escapicua(char[]);
int escapicua(char Vec[T]);
// ^
You forgot the ; after function forward declaration.

Resources