如何使用模块化方法将指针传递给每个函数

时间:2018-01-26 22:33:18

标签: c pointers parameter-passing modular-design

我将代码编写到一个文件中并编译并运行,但我应该将其称为模块化代码,其中将每个函数放在单独的.c文件中,其中.h表示包含和原型,但我是很难这样做因为我不应该在我的.h文件中有任何变量。如何在每个函数文件中正确传递指针

这是我的代码:

---------------------------------------------------------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);

-----------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

好的,这是你做的:

您希望该变量存在于每个人都可以看到的地方,并且您只希望存在一个实例。

你想要的是C中的extern关键字。把它放在变量定义的前面,它只是一个声明。

考虑以下陈述:

int object_count;

int object_count = 0;

它们会导致分配内存。但是,如果你把

extern int object_count;

在某个文件中,然后说&#34;有一个名为object_count的变量int将在某处创建(可能是另一个文件)。

这是一个例子,在文件main.c中,我说会存在一个变量,但这里没有定义该变量。它住在其他地方。

#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;
}

object_counter.c中你有变量的定义。因此,如果您愿意,该变量将存在于object_counter.o中。但是通过extern声明,其他模块可以访问它。

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

int object_count = 0;

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

这是输出,我修改main.c中的值,我们看到object_counter.c中函数使用的值是相同的。

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

这是你可以在C中共享变量的机制,有一点需要注意:定义应该总是在C文件中(你可以通过将它放在.h文件中但通过宏技巧来实现)。

这并没有描述最正确的方式或传统方式。