0

If I place my arrays outside of the main function as global variables, my Merge-Sort works. If i declare unsortedArray or tempArray or BOTH inside of the main function, the terminal lights up with undeclared variable errors.

From Javascript I thought that nested functions can access stuff declared at a higher level. Since the Main() function is the highest level declaring all these others, shouldn't the sort() and merge() have access to anything declared in Main?

Am I just passing the parameters in incorrectly with the wrong name?

I guess just for convenience I had to declare them globally so I can call them freely when needed.

Since I pass unsortedArray at sort(unsortedArray, Start, End);

Does this mean each time my subroutines call (Array[]) they are calling in unsortedArray[]? How do I get the second one in there, if I wanted to declare it locally?

// Arrays MUST be declared outside of Main to work here??
int tempArray[8] = {};
int unsortedArray[8] = {4,2,6,8,1,3,7,5};
void sort(int Array[], int Start, int End);
void merge(int Array[], int Start, int Middle, int MiddlePlus, int End);
int main(void)
{
    int Start = 0;
    int End = 7;
    sort(unsortedArray, Start, End);
}
void merge(int Array[], int Start, int Middle, int MiddlePlus, int End)
{
Does stuff with tempArray
Does stuff with unsortedArray
}

void sort(int Array[], int Start, int End) 
{
  if(End > Start)
  { 

  int Middle = ((Start + End) / 2);
  int MiddlePlus = Middle + 1;

  sort(Array, Start, Middle);
  sort(Array, Middle + 1, End);
  merge(Array, Start, Middle, MiddlePlus, End);


}

How do I declare those Arrays locally and properly?

1 Answer 1

0

C handles vars differently. Everything is contained in the local function (including main, which is essentially a function as well). A var declared within a function is only accessible within that function. Vars declared within a function (or in main) can not be accessed below that function or anywhere else outside of that function.

Global vars, on the other hand, can be accessed anywhere.

As for how to pass the vars, C passes by copy. A copy of the vars are passed when a function is called. You had it right, but had the internal usage wrong. Let's look at part of your pseudocode with some of my examples added:

int main(void)
{
    int Start = 0;
    int End = 7;
    sort(unsortedArray, Start, End);
}vars 


void sort(int Array[], int first, int last) 
{
    do something with unsortedArray;  // unsortedArray doesn't exist here

    do something with Array;     // this will work

    int count = End - Start;      // vars don't exist

    int count = last - first;     // this would be valid
    // nothing to return, but changes to unsortedArray would stick.       
}

Remember that copies of data are passed to functions through calls. In this case, main is passing copies of the contents of Start and End to the sort function. Simple enough. Arrays are a little differnet. The address of unsortedArray is passed to the function. Having the address of the original array, sort can then operate on the array.

Inside the function though, you have to use the same var names used in the signature of the function, not the var names in the call from the upper function or from main. Thus, in my example, Array[], first and last are valid inside the function, while unsortedArray, Start and End are not.

Finally, changing first and last will not change Start and End. However, since the address of the array is passed by copy, changing Array will also change unsortedArray.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Thanks SO much for the answer. This is incredibly helpful. There is still hope left, people still ~~care~~ swoon lol but seriously thanks
    – Mikhail
    Commented Oct 8, 2017 at 1:13
  • Is there anything wrong with using global variables? Like people say for JS using global variables is wrong because it pollutes global namespace or something, is there some similar problem?
    – Mikhail
    Commented Oct 8, 2017 at 1:16
  • Yes. While some say never, globals have their place. You should know how to use them. More importantly, globals should have a really good reason for being used and should only be used sparingly - something that will show up in a couple weeks. ;-) The problem is this. When teams are working on different parts of a large project, writing individual functions, for instance, they can create local vars of whatever names they wish as local vars inside the functions. (continues....)
    – Cliff B
    Commented Oct 8, 2017 at 1:25
  • (...continued) However, the moment a global is created, that name must not be used elsewhere unless it is deliberate use of the global. If one team member creates a global and calls it, for instance, i, then any usage of i by any other team member will either cause a compile error, or worse, introduce an inadvertent bug in the code. Also, when a global is correctly used, the entire team must be aware of it. So, unless there's a compelling reason for a global var, they should be avoided. If used, they should be used sparingly.
    – Cliff B
    Commented Oct 8, 2017 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .