How can I modify C variables in another file?

Programming, for all ages and all languages.
Post Reply
User avatar
Mikumiku747
Member
Member
Posts: 64
Joined: Thu Apr 16, 2015 7:37 am

How can I modify C variables in another file?

Post by Mikumiku747 »

Hello,
I'm sure this is a common question, but I had a look and couldn't find any obvious answers, so yeah. Anyways, I'm making the input handler for my kernel, and I have an array of characters that I'm storing the input in. Problem is, I need to modify this array from my interrupt handler, which is in a separate C file I've included into the main c file. However, I can't seem to edit the value from the interrupt handler, its having no effect.

Here's a quick example of the code I'd be using:

Code: Select all

//Main.c
#include "interrupts.c"

char *command[2000];
char *original = command;

int main() {
    //stuff
    for (int i = 0; i<2000; i++) {
        command[i] = 0;
    }
    while (*command != 'e');
    printf("command length %i\n", command-original);
}

Code: Select all

//interrupts.c

extern char *command;

void handle_kbd() {
    char kbd = read_keyboard();
    *command = kbd;
    command++;
}
Please try to ignore obvious spelling mistakes, the code is there for example.

Long story short, I'm modifiying the variable in the interrupt handler, but it has no effect. Im confused by this, and my first hunch was that it's 2 seperate varibless, but I used objdump and saw only one symbol called command In the resulting binary. So now I'm out of ideas and have come to you for help. I know my way around C, but i'm not sure why it's not working, And i haven't really worked with the extern keyword before. Also, for some reason, the linker throws a fit if I don't declare the variable in interrupts.c, even though it's getting included by the mainfile.

Does anybody have any ideas, I would really appreciate them :D
Thanks, Mikumiku747
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How can I modify C variables in another file?

Post by iansjack »

In main.c you declare command as a char **. In interrupts.c you use it as a char *. That's going to lead to unexpected behaviour.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How can I modify C variables in another file?

Post by Combuster »

What iansjack said, and Volatile
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Mikumiku747
Member
Member
Posts: 64
Joined: Thu Apr 16, 2015 7:37 am

Re: How can I modify C variables in another file?

Post by Mikumiku747 »

AH, it needs to be volatile, I didn't even consider that it might get optimized, but now that I look at it, it makes quite a lot of sense. Also, I typed out the code on my phone, so the double pointer is just a typo, it doesn't have the asterisk in the actual code. Thanks for the lesson, it's exactly what I was looking for. :)
Post Reply