0

I want to use an array from a function HashTableInit to printHash. So, I defined it globally. But array's parameters are present in function HashTableInit. I don't want to take array by returning the function, because to use return I will need to call the whole function in printHash, and it will disturb my whole code.

void HashTableInit(int TableSize, int Key) {
//Create a space for HashTable as given in variable TableSize
struct hashTableNode * HT[TableSize];
for (int i = 0; i < TableSize; i++) {
    HT[i] = (struct hashTableNode*) malloc(sizeof (struct hashTableNode));
    HT[i] ->Key = 0;
    HT[i] ->next = NULL;
}

Here, TableSize will be available after this function executes. But how could I define array globally with parameters.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • https://stackoverflow.com/questions/1856599/when-to-use-static-keyword-before-global-variables – Ôrel May 06 '18 at 17:18

1 Answers1

0

If it's not a pointer you can declare the variable with values with globally. But in your case, it's a pointer variable. You have to manually allocate address before assigning the value. So it's a good practice to create a function to allocate values to the pointer variables. But you can assign the array and a variable to store the array size globally.I hope it will help you.

  • Actually, I'm weak with pointing things. This always mess up my program. If you have an example of defining pointer function to get HashTable values. Then, I will be thankful. – umair mughal May 07 '18 at 14:29
  • I searched from internet but not found anything that I can understand. – umair mughal May 07 '18 at 14:30