0

我将代码写到一个文件中,然后编译并运行,但我应该把它分成称为模块化代码的片段,你将每个函数放在单独的 .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);

-----------------------------------------------------------------
4

1 回答 1

0

好的,这就是你要做的:

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

你想要的是externC 中的关键字。把它放在变量定义的前面,它就变成了一个声明。

考虑这些陈述:

int object_count;

或者

int object_count = 0;

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

extern int object_count;

在某个文件中,然后显示“有一个名为的变量object_countint它将在某处创建(可能是另一个文件)。

这是一个例子,在文件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);
}

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

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

这是您可以在 C 中共享变量的机制,需要注意的一点是:定义应始终位于 C 文件中(您可以通过将其放在 .h 文件中但通过宏技巧来实现)。

这没有描述什么是最正确的方法或传统的方法。

于 2018-01-26T22:58:12.430 回答