Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

SCOPING PROBLEM ONCE again ;-)

RIGHT_THENRIGHT_THEN Member Posts: 154
SCOPING PROBLEM ONCE again ;-)

GENTLEMEN :-D

it is a real pr:-(blem and i need s:-)lution to this Please:-

QUESTION:
HOW TO USE SOME VARIALBE IN ALL THE FILES OF A PROJECT

extern is not w:-(rking;
by what i have read of it.and understood
It Is SomeThing Which Makes A VariabLe Global Once.
and helps combine languages etc etc...

For Example

In SomeFile.h
[code]
//it is Defined
extern int ExternVariable;
[/code]
THEN
In SomeFile.cpp
[code]
#include "SomeFile.h"

Void SomeFuction(void)
{
int ExternVariable=1;
//now it has become local to this function
//and lost it Global DImension;
//i cannot use it successfully in someother file,Or can i;
//for example look below:-
}
[/code]

In SomeOtherFIle.cpp
[code]
#include "SomeFIle.h"
void OnceMore(void)
{

ExternVariable=2;//would produce a error Or not!;
//while linking it would say it is undefined
}
[/code]
NOW TO PUT A VARIALBE IN A HEADER FILE
so that it can be assesed by all the files which include that
header has its own problems Like Multiplicity of getting
defined that variable.
TO over come that #preprocessors are used
[code]
#ifndef
#define
#endif
[/code]
i used then too with no luck have a look at the code below
i included these statements in my header file so that
these variables get compiled only once;See if i did it right way
[code]
#ifndef hInstanceForCaret1Defined

HINSTANCE hInstanceForCaret1;
HBITMAP hBitMapForCaret1 ; //for loading caret;

#define hInstanceForCaret1Defined

#endif
[/code]
althou this HEADER file itself has this kind of
PreProcessor Statement for itself so it gets compiled once
Like this
[code]
#ifndef ThisHeaderHasBeenCompiled

#define ThisHeaderHasBeenCompiled

#endif
[/code]
Now This thing is working well for Functions but not
FOr these Two VAriaBLes;


AM i Understanding these PreProcessors well Or have i made
a Mistake;
kindly set me right
Because Not to Be able to use SOme Variable
in Multiple files
It WOuld Be impossible to DiViDe ProgRAMMM into Small
MOdules.:-(









Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2005-8-21 4:34:56[/red][/b][hr]
    The extern keyword means that the variable is going to be declared (without extern) in some other *.c or *.cpp file. Using extern in *.h is the correct solution. What you failed to do was declare it WITHOUT extern in the *.c or *.cpp file.



    : In SomeFile.h
    : [code]
    : //it is Defined
    : extern int ExternVariable; [red]<<< This is CORRECT[/red]
    : [/code]
    : THEN
    : In SomeFile.cpp
    : [code]
    : #include "SomeFile.h"
    : [red] int ExternVariable = 0;[/red]
    : Void SomeFuction(void)
    : {
    : int ExternVariable=1;
    : //now it has become local to this function
    : //and lost it Global DImension;
    : //i cannot use it successfully in someother file,Or can i;
    : //for example look below:-
    : }
    : [/code]



  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : [b][red]This message was edited by stober at 2005-8-21 4:34:56[/red][/b][hr]
    : The extern keyword means that the variable is going to be declared (without extern) in some other *.c or *.cpp file. Using extern in *.h is the correct solution. What you failed to do was declare it WITHOUT extern in the *.c or *.cpp file.
    :
    :
    :
    : : In SomeFile.h
    : : [code]
    : : //it is Defined
    : : extern int ExternVariable; [red]<<< This is CORRECT[/red]
    : : [/code]
    : : THEN
    : : In SomeFile.cpp
    : : [code]
    : : #include "SomeFile.h"
    : : [red] int ExternVariable = 0;[/red]
    : : Void SomeFuction(void)
    : : {
    : : int ExternVariable=1;
    : : //now it has become local to this function
    : : //and lost it Global DImension;
    : : //i cannot use it successfully in someother file,Or can i;
    : : //for example look below:-
    : : }
    : : [/code]
    :
    :
    :
    : THANKS AS ALWAYS SIR STOBER
    YOU ARE BEING KIND

    but i will have to get on to your nerves SIR
    because i need answers so.

    SO I am Persisting still with my question
    MY QUESTION IS HOW TO USE ONE VARIABLE AND USE IT IN TWO FILES
    AND VALUE GIVEN TO IT IN ONE FILE SHOULD BE SUSTAINED IN IT
    WHILE USING IT IN OTHER FILE.THAT IS NOT WHAT EXTERN IS DOING.
    OR MAYBE I AM NOT DOING IT RIGHT.

    IN SOMEFILE.H
    [CODE]
    HAVE A LOOK SIR
    extern int ExternVariable; //this a integer variable decalred here
    //to be used in files which will include
    //this header

    [/code]
    NOW
    WHAT IS HAPPENING IS THIS
    IN SOMEFILE.CPP
    [code]
    #include<SOMEFILE.h>
    void somefunction(void)
    {
    ExternVariable=200; //i have not used extern with it
    //"Extern"+"Variable"==ExternVariable is
    //the name of this variable i am not using
    //extern again with it.like this
    //extern int ExternVariable=200;
    //it is only ExternVariable=200;

    }
    [/code]
    NOW i want this 200 value to be sustained in it while i use it
    in SOMEOTHERFILE.CPP like this

    IN SOMEOTHERFILE.CPP
    [code]
    #include
    void SomeFunctionHere(void)
    {
    int IntegerVariable;

    /*
    USIng some function to get typed
    the value of ExternVariable
    */

    SomeTextOutFunction(ExternVariable); //it is not giving 200;
    //which i stored in it
    //above which means it does
    //not have the kind of
    //global scope i want

    /*
    if i use it in this file
    it might be global within
    this file only but not within
    two files

    */


    }

    [/code]

    NEcessity IS THE MOTHER OF INVENTION THEY SAY
    SO:-
    I had to RESORT TO UTILIZING FUNCTIONS TO TRANSFER values
    BETWEEN TWO FILES.BUT THAT IS AN expensive affair either
    i will have to write as many functions as many different
    types of value i want transffered from one file to another
    or just one function having that much many of parameters
    one for each kind of value.
    BUT
    LIKE IN VISUAL BASICS one declares a varialbe public then it
    is available inside all the modules within the application.
    something like that

    A VARIABLE POINTING AT ONE LOCATION AND WHENEVER WHICH FILE NEEDS
    IT. IT FETCHES THE VALUE FROM IT.IS SOMETHING LIKE THAT AVAILABLE IN
    C++.




  • MT2002MT2002 Member Posts: 1,444

    : but i will have to get on to your nerves SIR
    : because i need answers so.
    :
    : SO I am Persisting still with my question
    : MY QUESTION IS HOW TO USE ONE VARIABLE AND USE IT IN TWO FILES
    : AND VALUE GIVEN TO IT IN ONE FILE SHOULD BE SUSTAINED IN IT

    [blue]
    In "it"? In what? In the varable? If so, stober has already
    answered your question.[/blue]

    : WHILE USING IT IN OTHER FILE.THAT IS NOT WHAT EXTERN IS DOING.
    : OR MAYBE I AM NOT DOING IT RIGHT.

    [blue]
    See below..[/blue]

    : IN SOMEFILE.H
    : [CODE]
    : HAVE A LOOK SIR
    : extern int ExternVariable[red]=0; // should be initated.[/red]
    //this a integer variable decalred here
    : //to be used in files which will include
    : //this header
    :
    : [/code]
    : NOW
    : WHAT IS HAPPENING IS THIS
    : IN SOMEFILE.CPP
    : [code]
    [red]
    // "extern" tells the linker that it has external linkage.
    // It *does not* define it for the compiler. Hence, you have
    // to define it in *each* C/C++ file:

    int ExternVariable; //Note: no "extern" here[/red]

    [blue]
    // The linker will rsolve the name conflicts with
    // "ExternVarable", so everything should be okay.[/blue]

    : #include
    : void somefunction(void)
    : {
    : ExternVariable=200; //i have not used extern with it
    : //"Extern"+"Variable"==ExternVariable is
    : //the name of this variable i am not using
    : //extern again with it.like this
    : //extern int ExternVariable=200;
    : //it is only ExternVariable=200;
    :
    : }
    : [/code]
    : NOW i want this 200 value to be sustained in it while i use it
    : in SOMEOTHERFILE.CPP like this
    :
    : IN SOMEOTHERFILE.CPP
    : [code]
    : #include

    [red]// Again, you have to define it:

    int ExternVariable;[/red]

    : void SomeFunctionHere(void)
    : {
    : int IntegerVariable;
    :
    : /*
    : USIng some function to get typed
    : the value of ExternVariable
    : */
    :
    : SomeTextOutFunction(ExternVariable); //it is not giving 200;
    : //which i stored in it
    : //above which means it does
    : //not have the kind of
    : //global scope i want
    :
    : /*
    : if i use it in this file
    : it might be global within
    : this file only but not within
    : two files
    :
    : */
    :
    :
    : }
    :
    : [/code]
    :
    : NEcessity IS THE MOTHER OF INVENTION THEY SAY
    : SO:-
    : I had to RESORT TO UTILIZING FUNCTIONS TO TRANSFER values
    : BETWEEN TWO FILES.BUT THAT IS AN expensive affair either
    : i will have to write as many functions as many different
    : types of value i want transffered from one file to another
    : or just one function having that much many of parameters
    : one for each kind of value.
    : BUT
    : LIKE IN VISUAL BASICS one declares a varialbe public then it
    : is available inside all the modules within the application.
    : something like that
    :
    : A VARIABLE POINTING AT ONE LOCATION AND WHENEVER WHICH FILE NEEDS
    : IT. IT FETCHES THE VALUE FROM IT.IS SOMETHING LIKE THAT AVAILABLE IN
    : C++.

  • RIGHT_THENRIGHT_THEN Member Posts: 154
    :
    : : but i will have to get on to your nerves SIR
    : : because i need answers so.
    : :
    : : SO I am Persisting still with my question
    : : MY QUESTION IS HOW TO USE ONE VARIABLE AND USE IT IN TWO FILES
    : : AND VALUE GIVEN TO IT IN ONE FILE SHOULD BE SUSTAINED IN IT
    :
    : [blue]
    : In "it"? In what? In the varable? If so, stober has already
    : answered your question.[/blue]
    :
    WELL SIR MR.MT2002
    THNAKS FOR YOUR TIME FIRST OF ALL.
    it is not the first time i am asking this question it is the third
    time in my little life of about a week or so in this heaven.

    AND i came here thinking i dont know c.
    BUT NOW i have began to doubt my english too
    DO YOU KNOW SOME BOARD WHERE THEY TEACH ENGLISH
    so i can PHRASE MY QUESTIONS BETTER.

    IN MY desperation i have given up externing thing alltoghether
    and resorted to another way of getting data from a file
    through structure functions.and it is working well.


    IN STDAFX.H
    [code]
    struct WhatIsInMainFile{
    HINSTANCE GetMainInstance ;
    HWND MainWindowHandle ;

    };
    struct WhatIsInMainFile** StructFunc1(struct WhatIsInMainFile**);
    [/code]
    IN MAINFILE.CPP
    [code]
    struct WhatIsInMainFile** StructFunc1(struct WhatIsInMainFile
    **WhatIsInMainFile_Obj1)
    {

    (*WhatIsInMainFile_Obj1)->MainWindowHandle =MainHwnd ;
    (*WhatIsInMainFile_Obj1)->GetMainInstance =MainInstance1;

    return WhatIsInMainFile_Obj1;
    }
    [/code]

    IN Stdafx.cpp
    [code]
    WhatIsInMainFile **WhatIsInMainFile_obj1={0};
    WhatIsInMainFile *WhatIsInMainFile_obj2={0};

    WhatIsInMainFile_obj1 = (struct WhatIsInMainFile **)malloc
    (sizeof(WhatIsInMainFile));

    WhatIsInMainFile_obj2 = (struct WhatIsInMainFile * )malloc
    (sizeof(WhatIsInMainFile)) ;

    WhatIsInMainFile_obj1 = &WhatIsInMainFile_obj2 ;
    WhatIsInMainFile_obj1 = StructFunc1(WhatIsInMainFile_obj1);

    InstanceForCaret1 = WhatIsInMainFile_obj2->GetMainInstance ;

    //THIS IS WHERE IT WAS NEEDED FOR PASSing THE hInstance OF APPLICATION
    //so it is done now.Now i am getting it From GetMainInstance.


    hBitMapForCaret1 =(HBITMAP)LoadImage(
    InstanceForCaret1 ,
    MAKEINTRESOURCE(IDB_BITMAP1) ,
    IMAGE_BITMAP ,
    200 ,
    150 ,
    LR_VGACOLOR
    );




    [/code]

    ANOTHER THING I REALISED WHILE INTO ALL THIS GLOBAL SCOPING THING IS
    THAT CLASSES ARE POSSIBLE IN C TOO because i was unable to access
    a variable of one file from another.that is what a class is exactly
    having the capibility of hidding and not allowing its variables
    and functions to be accessed by others.same is the case with a file
    it contains eveything structures functions variables etc etc..

    which might or might not be global within it but can not be
    accessed from outside unless there is one function in it
    which has a header somewhere outside of it
    and that function when called gives the information what is
    inside that file as we might have programmed it to provide.
    just like classes one function at least has to be public

    so class is a sort of file within a file then! i dont know if
    that is making sense to you but i have developed some sense
    of it now some logics are clearing up.

    [CODE]
    BUT [size=5] E X T E R N

    IS
    NOT
    WORKING
    FOR
    ME [/size]
    [/CODE]
  • Griz803Griz803 Member Posts: 100
    Well, maybe trying a simpler program might clear this up a bit. Use these files and see if you get what you are looking for.

    extern.c file contains:
    [code]
    #include "extern.h"

    int main(void)
    {
    MyFirstExtern = 30;
    printf("First Extern = %d
    Second Extern = %d
    ",
    MyFirstExtern, MySecondExtern);
    MySecondExtern = 90;
    printf("
    First Extern = %d
    Second Extern = %d
    ",
    MyFirstExtern, MySecondExtern);
    system("PAUSE");
    return 0;
    }
    [/code]

    extern.h file contains:
    [code]
    #ifndef _EXTERN_H_
    #define _EXTERN_H_

    #include
    #include

    extern int MyFirstExtern;
    extern int MySecondExtern;


    #endif
    [/code]

    extern2.c file contains:
    [code]
    int MyFirstExtern = 20;
    int MySecondExtern;

    MySecondExtern = 50; /* You may get a warning here. */
    [/code]

    My ouput was:

    First Extern = 30
    Second Extern = 50

    First Extern = 30
    Second Extern = 90
    Press any key to continue...

    What I think you are not seeing is that you declare the variable once, declare it as extern once and initialize it and use it where ever after that. It is global, so to speak. You still can't initialize it twice and expect it to maintain a value from before, or redeclare it.
  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : Well, maybe trying a simpler program might clear this up a bit. Use these files and see if you get what you are looking for.
    :
    : extern.c file contains:
    : [code]
    : #include "extern.h"
    :
    : int main(void)
    : {
    : MyFirstExtern = 30;
    : printf("First Extern = %d
    Second Extern = %d
    ",
    : MyFirstExtern, MySecondExtern);
    : MySecondExtern = 90;
    : printf("
    First Extern = %d
    Second Extern = %d
    ",
    : MyFirstExtern, MySecondExtern);
    : system("PAUSE");
    : return 0;
    : }
    : [/code]
    :
    : extern.h file contains:
    : [code]
    : #ifndef _EXTERN_H_
    : #define _EXTERN_H_
    :
    : #include
    : #include
    :
    : extern int MyFirstExtern;
    : extern int MySecondExtern;
    :
    :
    : #endif
    : [/code]
    :
    : extern2.c file contains:
    : [code]
    : int MyFirstExtern = 20;
    : int MySecondExtern; //<--Putting =50 here it self-----<|
    : ^
    : MySecondExtern = 50; /* You may get a warning here. */|
    : /* yes it rather gives error here ^
    |
    remedy is -->---->-->-->------->-*/
    [/code]
    :
    : My ouput was:
    :
    : First Extern = 30
    : Second Extern = 50
    :
    : First Extern = 30
    : Second Extern = 90
    : Press any key to continue...
    :
    : What I think you are not seeing is that you declare the variable once, declare it as extern once and initialize it and use it where ever after that. It is global, so to speak. You still can't initialize it twice and expect it to maintain a value from before, or redeclare it.
    :

    ======My Portion=======================
    [code]
    WHILE(1)
    {
    printf("%X",THANKS_ A_ MILLION * MILLION);
    }
    [/code]


    many many thanks Sir Mr.Griz :-D :-D :-D :-D :-D
    Deepest DeepesTEST appericiations !!!!

    My deepest APPRECIATIONS FOR YOUR time
    and logic as well as the pains for that little programm.
    you arthitected for sampling the problem.you have been
    most generous.

    First Time I have A Handle On Extern;
    it bEhaves Diffrently when placed at different places
    but i will mutilate it myself n:-ow to get it burning f:-or
    my use :-) .althou i am not confident with it still because mearly
    declaring it in function or outside changes its scope.but anyway
    i am not keen to Doctrate on it as of this moment.
    but when i wish to utilize it in future i now know how to get
    started with it.Did i thank you for that! if not Zillions THanks.

    now i can understand what other gentlemen were trying to convince
    me with.But Just Like tuBelight i guess i burn late.

    HOPE T:-) B:-)THER Y:-)U AGAIN IN FUTURE .MIGHT BE WITH EXTERN
    AGAIN:-D





  • stoberstober Member Posts: 9,765 ✭✭✭
    : mearly declaring it in function or outside changes its scope.

    That's correct. If you put the declaration of the int inside a function then it will not be visible to other functions no matter what file they are in.

  • islam ahmadislam ahmad Member Posts: 47
    : SCOPING PROBLEM ONCE again ;-)
    :
    : GENTLEMEN :-D
    :
    : it is a real pr:-(blem and i need s:-)lution to this Please:-
    :
    : QUESTION:
    : HOW TO USE SOME VARIALBE IN ALL THE FILES OF A PROJECT
    :
    : extern is not w:-(rking;
    : by what i have read of it.and understood
    : It Is SomeThing Which Makes A VariabLe Global Once.
    : and helps combine languages etc etc...
    :
    : For Example
    :
    : In SomeFile.h
    : [code]
    : //it is Defined
    : extern int ExternVariable;
    : [/code]
    : THEN
    : In SomeFile.cpp
    : [code]
    : #include "SomeFile.h"
    :
    : Void SomeFuction(void)
    : {
    : int ExternVariable=1;
    : //now it has become local to this function
    : //and lost it Global DImension;
    : //i cannot use it successfully in someother file,Or can i;
    : //for example look below:-
    : }
    : [/code]
    :
    : In SomeOtherFIle.cpp
    : [code]
    : #include "SomeFIle.h"
    : void OnceMore(void)
    : {
    :
    : ExternVariable=2;//would produce a error Or not!;
    : //while linking it would say it is undefined
    : }
    : [/code]
    : NOW TO PUT A VARIALBE IN A HEADER FILE
    : so that it can be assesed by all the files which include that
    : header has its own problems Like Multiplicity of getting
    : defined that variable.
    : TO over come that #preprocessors are used
    : [code]
    : #ifndef
    : #define
    : #endif
    : [/code]
    : i used then too with no luck have a look at the code below
    : i included these statements in my header file so that
    : these variables get compiled only once;See if i did it right way
    : [code]
    : #ifndef hInstanceForCaret1Defined
    :
    : HINSTANCE hInstanceForCaret1;
    : HBITMAP hBitMapForCaret1 ; //for loading caret;
    :
    : #define hInstanceForCaret1Defined
    :
    : #endif
    : [/code]
    : althou this HEADER file itself has this kind of
    : PreProcessor Statement for itself so it gets compiled once
    : Like this
    : [code]
    : #ifndef ThisHeaderHasBeenCompiled
    :
    : #define ThisHeaderHasBeenCompiled
    :
    : #endif
    : [/code]
    : Now This thing is working well for Functions but not
    : FOr these Two VAriaBLes;
    :
    :
    : AM i Understanding these PreProcessors well Or have i made
    : a Mistake;
    : kindly set me right
    : Because Not to Be able to use SOme Variable
    : in Multiple files
    : It WOuld Be impossible to DiViDe ProgRAMMM into Small
    : MOdules.:-(
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :














    C language is case sensitve so uppercase & lowercase may not be used simultaniously

  • RIGHT_THENRIGHT_THEN Member Posts: 154



    [blue]
    C language is case sensitve so uppercase & lowercase may not be used
    simultaniously[/blue]
    [code]
    #include
    #include
    #include
    void main()
    {
    int AcedFGH = 0 ;
    int DFedfS = 10 ;
    int CcSeGH = 20 ;
    int Result1 ;
    clrscr();
    Result1 = AcedFGH+DFedfS+CcSeGH ;
    printf("%d=Result",Result1) ;
    getch();
    }
    Now Sir Have A Look above and see Mixed Case WOrks Well in c.
    Case Sensitivity is that, a Variable name written in which case
    will be excepted in that case only and not in other case.
    Case Sensitivity Does Not mean that ALL the ALphabets in a
    String Have to be Either Small Or Upper BUt it MEans
    if SOme thing is In Lower CAse Than THat CAse Has TO
    Be MainTained For thaT VAriable Otherwise it would be considered
    yet another variable name which if not declared will result in
    error now i could be wrong but
    experience Shows it to be right Above ProgRamm is the example;
    Or YOU WERE Reffering SOmething Else!!!


    [/code]
Sign In or Register to comment.