0

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.

Ivickt
  • 29
  • 5
  • The full code you are showing does not include the definition of a and b. You did say you define them at the top of the code, but the presented code does not show that (this covers the 1st compiler error). The 2nd one hints that you did include a and b, but you have not given them any values before passing them to the compare_quads() function. – Harry K. May 23 '21 at 23:43
  • Why don't you explain what exactly you're trying to do? Currently, what you're doing is calling `compare_quads` on two variables which you haven't defined and then throwing away the result. This doesn't make any sense to me. – Mark Saving May 23 '21 at 23:44
  • i updated my question to explain what it does. and i thought by putting the int compare_quads(const void *a, const void*b); was declaring the variable. also without the compare_quads function the sorting doesn't work. – Ivickt May 23 '21 at 23:56
  • It seems to me that you have no reason to call `compare_quads` in `main` at all. The only purpose of `compare_quads` is to be called by `qsort` from `compare()` (which is sort of a poor name for a function whose job is to sort). So the line `compare_quads(a, b);` in `main` serves no purpose and makes no sense. Just remove it and then think about what's left. – Nate Eldredge May 24 '21 at 00:02
  • If you have more questions after that, it would be helpful if you would provide an input file that demonstrates the problem, and explain how the outcome differs from what you wanted. – Nate Eldredge May 24 '21 at 00:04
  • Again, the warning tells you that you are trying to use a and b without having assigned them a value first. It's like having a function that sums 2 integer a and b, and you pass it a and b without values (tho global vars are auto-zero'ed). Anyway, your compare_quads() func is pretty useless in the presented code, but I'm just explaining what the compiler warning tells you. – Harry K. May 24 '21 at 00:06
  • Even if you declare a/b constants, compare_quads expects void pointers, so it shuld be called as `compare_quads(&a,&b)`, but why are you using memcmp? – xvan May 24 '21 at 00:07
  • i thought i needed that bit of code to make the qsort function properly. and if i remove the compare_sort (a,b) (which i agree i named badly) then when i execute the code it will not have the function executed and i'll be left with a blank output, as far as i know. – Ivickt May 24 '21 at 00:15

1 Answers1

0

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.

dbush
  • 205,898
  • 23
  • 218
  • 273