Jump to content

Help with C


Robert

Recommended Posts

Hope this is the right forum??

 

OK here's what i want to do. I want to share a variable between 2 source files, let's call them 1/a.c and 2/a.c, and there's a 3/a.h as well. I thought I could create the variable in a.h and it would be available in the other 2 but it doesn't work. And if I altered it in 1/a.c, that change should reflect in 2/a.c

 

Instead all I get is a whole lot of frustrating errors. :)

 

What do I do?

Link to comment
Share on other sites

i dont understand your problem entirely but i think this is what you are talking about:

 

/////////////// sc 3.h ///////////

 

#include<stdio.h>

 

int a=10;

 

 

////////// sc: 1.c ////////////

 

#include<stdio.h>

#include<c:\tc\bin\3.h>

 

exe_test()

{

a++;

printf("%d",a);

a++;

}

 

 

//////////// sc: 2.c////////////

 

#include<stdio.h>

#include<conio.h>

#include<c:\tc\bin\1.c>

 

main()

{

clrscr();

exe_test();

printf("\n%d",a);

getch();

}

 

 

is this the kind of problem you are faced with?

this is clearly working

Link to comment
Share on other sites

can't you make structs in C? Have you tried making a header with a struct as a sort of database?

 

for ex

 

/*This is 3.h*/

struct Variables
{
    int A;
    int B;
    unsigned short C;
};

 

then in 1.c you can do

 

/*This is 1.c*/
#include <stdio>
#include "3.h"

int main()
{
    Variables Vars; //this would statically allocate memory for the struct in 3.h
    printf("\nEnter a number: ");
    scanf(Vars.A);
    
    /*something like that rather, I don't know much about C*/
    return 0;
}

 

then possibly use those same variables in 2.c

Edited by Weirdy
Link to comment
Share on other sites

I think of two possible problems:

 

1) You are using Turbo C++ and you hadn't made a project and placed the two source files in it.

 

2) You tried to run your two source files at the same time. Different programs use different memory location for their variables. Even if they are two instances of the same code.

Link to comment
Share on other sites

I don't understand any of what you said, I'm a rank amateur at this. It's not C++, it's just C.

 

I got it to work by entering it in the h file, and declaring it again in one of the c files, and then the other c file could see it too and it worked.

 

The problem was in the h file I was trying to assign a value to a variable which is not allowed. The error message didn't make that clear at all.

 

I'm confused by looking at other people's stuff, sometimes they have *var and &var, and sometimes just the var, and sometimes with extern in front of it. When should I use one form or another?

Link to comment
Share on other sites

I'm confused by looking at other people's stuff, sometimes they have *var and &var, and sometimes just the var, and sometimes with extern in front of it. When should I use one form or another?

 

If you just use var and change the variable inside the function, whatever you sent to the function will not change. If you use &var, any change to the variable inside the function will change the variable you sent to the function. This means you must send a variable to the function, not an expression.

 

*var is sending the pointer to the variable. If you have a variable, you can get the pointer to it with &var(do not confuse with the & of attribute declaring), and if you have a pointer, you can access the variable it is pointing to with *var(this is also the syntex for declaring a pointer variable).

 

&var is only available in C++, and it is simply an easier way for sending the pointer.

 

 

This is the first time I hear about extern. I found this on google:

When you declare a variable as extern your program doesn't actually reserve any memory for it, extern means that the variable already exits external to the function or file.

 

If you want to make a variable available to every file in a project you declare it globally in one file, that is, not inside a function, and add an extern declaration of that variable to a header file that is included in all the other files.

 

 

 

BTW, help us helping you by posting here your code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...