1

I am trying to implement the hacker3 sorting algorithm. The one that requires a O(n). I think I have figured out the algorithm but I won't spoil it by posting any code here.

My question is regarding to the inputs. It says that we are not allowed to change the definition of the functions and that the maximum available value constant can be used. I should mention here that the number is given as a const int and is not defined as an alias.

The problem arises at the point where we need to access that number from a library declared before the number. The only solution I figured out was to re-declare it in the library, this implies that a change of that number per-compilation would be impossible to detect. Is there any way to actually access it? Or should we just live with the re-declaration?

2
  • Is that what you're looking for?
    – kzidane
    Commented Oct 11, 2014 at 19:55
  • I don't think that helps much as I can't modify the file containing the global variable, unless I did not understand how you suggest I use extern. Do you have something particular in mind? If not don't bother about it, I just assumed that it can be redefined locally in the headers.c file, its a constant anyway... I don't even think there is a way to access it but thanks. Commented Oct 11, 2014 at 20:21

1 Answer 1

1

Apparently, you need to access the constant global int namely MAX which is defined in find.c from other source files. You could do something similar to that

mod1.c

#include <stdio.h>

extern int MAX; // declare MAX to be used externally in mod1.c

void foo(void)
{
    printf("MAX from foo is %i\n", MAX); // access MAX from mod1.c
}

mod1.h

void foo(void); // declare foo

mod2.c

#include <stdio.h>

extern int MAX; // declare MAX to be used externally in mod2.c

void bar(void)
{
    printf("MAX from bar is %i\n", MAX); // access MAX from mod2.c
}

mod2.h

void bar(void); // declare bar

main.c

#include <stdio.h>
#include "mod1.h"
#include "mod2.h"

int MAX = 65536; // define MAX

int main(void)
{
    printf("MAX from main is %i\n", max);
    foo();
    bar();
}

Output:

MAX from main is 65536
MAX from foo is 65536
MAX from bar is 65536

You may refer to this answer for more information on the topic!

4
  • You are right! This is exactly what I was looking for, thank you for your response. This was not covered in the lectures yet though. So extern just declares something which is defined somewhere else? It is just a compiler instructive keyword and it doesn't change anything in compilation, correct? Commented Oct 12, 2014 at 3:25
  • @AlexandrosAndreou apparently, this isn't covered in any of the video resources. However, it's covered in the readings. The keyword extern just signals to the system that a globally defined variable from another file is to be accessed.
    – kzidane
    Commented Oct 12, 2014 at 3:58
  • ok, even though I did not really know what extern was I had a sense that it would just store the variable in some other part of memory. Probably some globally accessible memory while technically if I understood it right, it says go look at that globally accessible memory for its definition as it is given in some other file and it can't be found here. I hope I understood it right. You said it is covered in the readings. I probably lost something in the process, where are the readings? Is there a text book or do you mean the problem sets? Thanks again! Commented Oct 12, 2014 at 14:22
  • @AlexandrosAndreou honestly, I don't know much about what's going on underneath the hood, but basically, when you define a global variable, it's actually not just a global variable, but also an externally global variable. All the keyword extern basically does is that it enables you to refer to that variable from another source file. As for the readings, there are recommended textbooks and other online readings. See the "Books" section in the syllabus for more info!
    – kzidane
    Commented Oct 12, 2014 at 15:17

You must log in to answer this question.

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