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.