#include <stdio.h>
#define MAX 9
void main (int argc, char *argv[]) {
printBoard();
}
void printBoard(void) {
int row,col;
row=col=0;
for(row;row<MAX;row++) //row navigation
for(col;col<MAX;col++){//column navigation
printf("r:%d,c:%d",row,col);
}/*End Column Nav*/
printf("\n");
}
I'm not sure what I am doing wrong here - the error I get :
"warning: conflicting types for ‘printBoard’ [enabled by default]
note: previous implicit declaration of ‘printBoard’ was here"
Try adding a function prototype for printBoard above main() e.g.,
void printBoard(void);
void main(...)
You have declared function after calling it.
#include <stdio.h>
#define MAX 9
void printBoard(void) {
int row,col;
row=col=0;
for(row;row<MAX;row++) //row navigation
for(col;col<MAX;col++){//column navigation
printf("r:%d,c:%d",row,col);
}/*End Column Nav*/
printf("\n");
}
void main (int argc, char *argv[]) {
printBoard();
}
This should work pretty fine.
Edit: You should declar all function before calling any of them.
Like void printBoard(void);
You are calling the method before it is declared.
Solve the problem by:
1) Moving the definition of void printBoard(void) above main or
2) adding a declaration above main. Just this line: void printBoard(void);
Related
i am trying to call a function in main so that my code will execute through all parts of my code. Now when i call for compare_quads in main. i am getting an error code of a and b not being declared. but my problem is that i do not know how to get the variable declared because i have declared the function and variable at the top of the code i thought that would work. and if i try declaring the variable in main like
const void *a;
const void *b;
then when compiling i receive, warning a is used uninitialized in this function, and similarly with b.
here is my code,
//declare libraries
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//declare other functions/files to be used in the program
void print_fun(void);
void read_fun(void);
static int compare(int arg, unsigned char networks[arg][4]);
int compare_quads(const void *a, const void *b);
//read command line input and store the information
int main(int argc, char** argv){
//declar variable
int arg = 0;
//make argv into an int
arg = atoi(argv[1]);
//assign size to networks
unsigned char networks[arg][4];
//assign input to networks
for (int j =0; j<1; ++j){
if(argc == 1)
{
printf("ERROR ERROR, you messed up\n");
}
else
{
// hold network addresses in a 2-d array, with 4 unsigned char
for(int k = 0; k<arg; k++){
for (int i =0; i<4; i++){
scanf("%hhu.", &networks[k][i]);
//checks to see if scanf was working properly
// printf(" %hhu",networks[k][i]);
}
//printf("\n");
}}}
compare_quads(a, b);
compare(arg, networks);
return(0);
}
int compare_quads( const void *a, const void *b) {
return memcmp (a, b, 4);
}
static int compare(int arg, unsigned char networks[arg][4])
{
qsort(networks, arg, sizeof(networks[0]), compare_quads);
for (int k = 0; k< arg; k++){
printf("%d.%d.%d.%d\n", networks[k][0],networks[k][1],networks[k][2],networks[k][3]);
}
return 0;
}
I am pretty new to c, so please let me know if you need any clarification. thank you.
the exact warnings are
unitilazed
main.c: In function ‘main’:
main.c:47:19: error: ‘a’ undeclared (first use in this function)
47 | compare_quads(a, b);
| ^
main.c:47:19: note: each undeclared identifier is reported only once for each function it appears in
main.c:47:22: error: ‘b’ undeclared (first use in this function)
47 | compare_quads(a, b);
| ^
when const void *a; is used to initalize.
main.c: In function ‘main’:
main.c:48:5: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
48 | compare_quads(a, b);
| ^~~~~~~~~~~~~~~~~~~
main.c:48:5: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]
EDIT
I am taking in one input file that has, a various amount of lines with network address like
139.72.16.202
i am storing the values in an array of size [variable that is set by arg][4]
then after the main function the rest i am using to sort the code by column. the sorting function worked fine.
A pointer to the compare_quads function is getting passed to the qsort function as a comparison function, so it will get called internally by qsort.
Because of this, you don't need to call compare_quads in your main function. Passing it to qsort is enough.
#include <stdio.h>
void fun();
int main(void) {
fun(fun());
return 0;
}
void fun()
{
printf("function is called");
}
The return type of function fun is void. So the below statement should be valid right!
fun(fun())
But the compiler raises compilation error as error: argument type 'void' is incomplete. Cannot understand what the error means?
I think you want something like this:
#include <stdio.h>
int fun(int arg); // function takes one argument
int main(void) {
fun(fun(1)); // use one argument when calling fun
return 0;
}
int fun(int arg)
{
printf("function is called\n");
return 0;
}
I am new to the concept of character pointers and strings.So, having trouble in passing a string pointer and returning a string pointer in a function.the function is "remove".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* remove(char* ,char ,int );
int main(){
int count, i;
char ch;
char* a;
char* b;
b=(char*)malloc(sizeof(char)*10);
a=(char*)malloc(sizeof(char)*10);
gets(a);
for(i=0;*a='\0';i++)
{
ch=a[i];
b=remove(&a[i],ch,i);
}
}
char* remove(char* str,char x,int k)
{
char* str2=(char*)malloc(sizeof(char)*10);
int i,j;
j=0;
int len;
len=strlen(str);
for(i=k;i<len;k++)
{
if(str[i]!='x')
{
str2[j]=str[i];
j++;
}
}
return str2;
}
The errors which i am getting are
error:conflicting types for 'remove'
In the line where the function was declared and defined in the subsequent line.
Function remove() is already defined in stdio.h.
int remove(const char *pathname);
Please give some other name to your function char* remove().
Note : Whenever you get such error, please try to look at manual page. If you are in unix. Simply type man func_name in terminal.
remove() is a standard function. You need to choose a different name for your remove() function name. Because your function definition for remove() differs with the prototype found in stdio.h.
I'm trying to populate a global int variable by passing command line arguments to a function. When I do this, I get warnings (see below), as well as a funky return number (such as 52 instead of the expected 49).
Any hints would be greatly appreciated. This is HW - but only a very small portion of the overall assignment.
#include <stdio.h>
#include <stdlib.h>
#include "kangarooHeaders.h"
int numJoeys = MIN_NUM_LEGAL_JOEYS - 1;
int main (int argc, char* argv[])
{
initializeNumJoeys(argc,argv);
printf("%d", numJoeys);
}
void initializeNumJoeys(void argc, void *argv[])
{
char line[LINE_LEN];
if (argc > MAMAS_NUM_JOEYS_CMD_LINE_INDEX)
numJoeys = *argv[1];
}
argv_test.c:13: warning: conflicting types for ‘initializeNumJoeys’
argv_test.c:9: warning: previous implicit declaration of ‘initializeNumJoeys’ was here
Put this above the main() function
void initializeNumJoeys(int argc, char *argv[]);
the reason is implicit function declaration, the compiler doesn't find a prototype for initializeNumJoeys() and implicitly declares it as
int initializeNumJoeys();
so when it finds the definition, then it's conflicting with the previous declaration.
Also, change this
numJoeys = *argv[1];
to
numJoeys = strtol(argv[1], NULL, 10);
and also, the function signature is wrong
void initializeNumJoeys(void argc, void *argv[])
/* ^ should be int */
so change it to
void initializeNumJoeys(int argc, void *argv[])
don't forget to fix the prototype.
I tried to the identifier function try_to_change_it() with two styles as below, it produced the same result. Which style is recommend?
#include "stdafx.h"
#include <stdio.h>
// style1 declare try_to_change_it() here
void try_to_change_it(int);
int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
// style2 declare try_to_change_it() here
void try_to_change_it(int);
printf("%d\n", a);
try_to_change_it(a);
printf("%d\n", a);
return 0;
}
void try_to_change_it(int a)
{
a = 777;
}
It makes no real difference. In a typical case, you declare functions by including a header, which you normally want to do outside any function.