0

I wrote code into one file and it compiled and ran, but I am supposed to turn in it in pieces called modular code where u put each functions in separate .c files with a .h indicating includes and prototypes, but I am having a hard time doing so because I am not supposed to have any variables in my .h file. How would I properly pass my pointer throughout each function file

Here is my code:

---------------------------------------------------------main.c

#include "my.h"
int *pointerNUM; //<----------------------------------------------here is the pointer
// main function
int main(int argc, char *argv[])
{
  // To store the numbers of number in file
  int numberOfNUM;
  // Calls the function to read numbers and stores the length
  numberOfNUM = readFile(argc, argv);
  // Calls the function to displays the numbers before sorting
  printf("\n Before sort : ");
  show(numberOfNUM);
  // Calls the function for sorting
  insertSORT(numberOfNUM);
  // Calls the function to displays the numbers after sorting
  printf("\n After sort: ");
  show(numberOfNUM);
}// End of main function

--------------------------------------------------------------insertSORT.c

#include "my.h"
// Function for insertion sort
void insertSORT(int numberOfNUM)
{
  int x, key, y;
  // Loops numberOfNUM times
  for (x = 1; x < numberOfNUM; x++)
  {
    // Stores i index position data in key
    key = pointerNUM[x];
    // Stores x minus one as y value
    y = x - 1;

    /*
    Move elements of pointerNUM[0..x - 1], that are greater than key,
    to one position ahead of their current position
    */
    while (y >= 0 && pointerNUM[y] > key)
      {
      // Stores pointerNUM y index position value at pointerNUM y next index position
      pointerNUM[y + 1] = pointerNUM[y];
      // Decrease the y value by one
      y = y - 1;
    }// End of while
    // Stores the key value at pointerNUM y plus one index position
    pointerNUM[y + 1] = key;
  }// End of for loop
}// End of function

-------------------------------------------------------------readFile.c

#include "my.h"
// Read in the parts file and returns the length
int readFile(int argc, char *argv[])
{
  // File pointer
  FILE *fptr;
  // numberOfNUM for number of numbers
  // cntVAR for counter variable
  int numberOfNUM, cntVAR;
  // Open the file for reading
  fptr = fopen(argv[1], "r"); // "r" for read

  // Check that it opened properly
  if (fptr == NULL)
  {
    printf("Cannot open file \n");
    exit(0);
  }// End of if condition
  // Reads number of numbers in the file
  fscanf(fptr, "%d", &numberOfNUM);

  // Dynamically allocates memory to pointer pointerNUM
  pointerNUM = (int *) calloc(numberOfNUM, sizeof(int));
  // Loops numberOfNUM times
  for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
    // Reads each number and stores it in array
    fscanf(fptr, "%d", &pointerNUM[cntVAR]);
  // Returns the length of the numbers
  return numberOfNUM;
  fclose(fptr);
}// End of function

----------------------------------------------------------------------show.c

#include "my.h"
// Function to show numbers
void show(int numberOfNUM)
{
  int cntVAR;
  // Loops numberOfNUM times
  for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
  // Displays each number
    printf("%4d, ", pointerNUM[cntVAR]);
}// End of function

-----------------------------------------------------------------Now my my.h

#include
#include

//prototypes
void insetSORT(int numberOfNUM);
int readFile(int argc, char *argv[]);
void show(int numberOfNUM);

-----------------------------------------------------------------
DYZ
  • 55,249
  • 10
  • 64
  • 93
John Meff
  • 23
  • 5
  • 1
    What exactly is the problem? Why is it harder to pass a pointer as a function parameter than it is to store it in a file-scope variable and have your functions retrieve it from there? – John Bollinger Jan 26 '18 at 22:37
  • Unrelated, but important: *please* learn to indent your code according to block nesting. Doing so greatly improves its readability. – John Bollinger Jan 26 '18 at 22:40
  • ask a specifc question. Like 'How do I pass variable x from func a to func b and retunr updated foo?' SO is not a c tutorial site – pm100 Jan 26 '18 at 22:48
  • 1
    The question is poorly worded but is really "how do you access a variable declared in a different translation unit" [and the answer is `extern`](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files?rq=1). But as @JohnBollinger suggested, a much better approach is to pass the pointer into each function as an additional argument ([say no to global state](https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil)). – TypeIA Jan 26 '18 at 22:52
  • I guess that could be what he means, @TypeIA, but that's not what I get from "I am not supposed to have any variables in my .h file" nor from the general idea of modularity to which he is being introduced. – John Bollinger Jan 26 '18 at 23:10

1 Answers1

0

Ok, here's what you do:

You want that variable to live somewhere that everyone can see it and you want only one instance of it to exist.

The thing you want is the extern keyword in C. Put this in front of a variable definition and it becomes just a declaration.

Consider these statements:

int object_count;

or

int object_count = 0;

They cause memory to be allocated. However, if you put

extern int object_count;

in some file, then that says "There is a variable called object_count that is an int that will be created somewhere (which could be another file).

Here is an example, in the file main.c, I say that there will exist a variable, but that variable is not defined here. It lives somewhere else.

#include <stdlib.h>
#include <stdio.h>

\\ This could be in an included .h file.
extern int object_count;
void print_object_count();


int main(void)
{
    object_count = 3;
    print_object_count();
    printf("Object count from main : %d\n", object_count);
    return 0;
}

And in object_counter.c you have the definition of the variable. Therefore that variable lives in object_counter.o if you will. But with the extern declaration, other modules have access to it.

#include <stdlib.h>
#include <stdio.h>

int object_count = 0;

void print_object_count()
{
    printf("print_object_count(): %d\n", object_count);
}

Here is the output, I modify the value in main.c and we see that the value that is used by the function in object_counter.c is the same one.

$ ./a.out
print_object_count(): 3
Object count from main : 3

This is the mechanism by which you can share a variable in C, one thing to note: the definition should always be in the C file (you may achieve that by putting it in an .h file but through macro tricks).

This does not describe what is the most correct way or conventional way of doing this.