Thread: Structures? *brain melts*

  1. #61
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    DoomMaster, I don't know what you mean by mainline. Explain?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #62
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    55 replies already? And you are getting any information out of all this nonsense Now you probably got bubba started with assembly stuff

    Hehe. Welcome to the fray Jawib. Feel free to jump in.

  3. #63
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by Lurker
    The stack is (hmm how to explain ) basically a list that can have items dynamically added / removed from it. The local variables are placed on the stack until the function returns, where the locals are removed from the stack.
    so is "stack" the L1 or L2 cache on the processor? lol...

    and could someone explain the difference between static and dynamic in terms of storing information? I thought I knew, but apparently I don't.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  4. #64
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    But of course, declaring a variable in the mainline would make it global also, right?
    If you mean inside the main() function, then no...It all has to do with scoping. Just because it hasn't been destroyed doesn't mean you can treat it as global. You have to pass values to the function so the function knows what the variable's value was, and if you need to change the variable...well...thats where pointers come in
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #65
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by bennyandthejets
    DoomMaster, I don't know what you mean by mainline. Explain?
    I believe he means int main().

    If so, then no, variables created in main are also, in essence, local. They cannot be used in other funtions. Global variables would be the bold ones below:
    Code:
    #include <iostream>
    using namespace std;
    
    int GLOBAL = 49;
    int main() {
     cout << "Global variable GLOBAL = " << GLOBAL << endl;
     return 0;
    }
    EDIT: ARGH stop posting all at once everyone .
    Last edited by Lurker; 11-01-2003 at 05:53 PM.
    Do not make direct eye contact with me.

  6. #66
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    DoomMaster, I don't know what you mean by mainline. Explain?
    Mainline is, the main function that contains all other functions... I guess in C++, it would be outside of everything... for example:

    Code:
    //headers go here
    
    int globalvariable;        //this would initialize a global variable, because it has initialized it outside of all functions, so therefore any function can refer to it
    
    int main()
    {
            //code
    }
    
    int function1()
    {
           //more code
    }

    kinda get what I'm saying now? I'm not sure if I'm correct or not, but I think I'm close at least!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  7. #67
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Your code is correct, but the term mainline is not proper terminology. I would say global instead.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #68
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Yes, by your terminology, variables in the mainline (usually called global) can be used by all the functions.
    Do not make direct eye contact with me.

  9. #69
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Think we need to explain variable scope or variable lifetime.



    Code:
    int x;
    
    
    int main(void)
    {
      return(0);
    }
    
    void FooBar(void)
    {
    }
    Here x has file scope. Every function can access x. It is global to the file. However, other file's cannot access x. It is only known in this file.

    Code:
    
    int main(void)
    {
      FooBar();
      return(0);
    }
    
    void FooBar(void)
    {
      int x;
    }
    This x has function scope. It is only known inside of FooBar. When you get back to main() x does not exist and cannot be accessed.

    Code:
    
    int main(void)
    {
      if (condition)
      {  
         int x=0;
      }
     
    return(0);
    }
    This x has block scope. It is only known inside of the {} block.

  10. #70
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    Your code is correct, but the term mainline is not proper terminology. I would say global instead.
    well actually mainline is the global area. That's what I've been taught anyway. Mainline is usually the main function or module in other programming languages, but in C++ I guess it would be the area that isn't inside of ANY function, usually at the top or bottom. This is what I believe, but sorry. I should use words everyone else understands
    --MrDoomMaster
    The kind of DooM that makes the MooD

  11. #71
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    However, other file's cannot access x. It is only known in this file.
    What about 'extern' Bubba . I think we should ignore extern variables for now.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #72
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Don't confuse him! He's starting to understand .
    Do not make direct eye contact with me.

  13. #73
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    What about 'extern' Bubba . I think we should ignore extern variables for now.


    Yeah I thought about that as soon as I wrote it...but yeah think we'll leave the extern discussion out right now. LOL

  14. #74
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by Bubba
    Think we need to explain variable scope or variable lifetime.



    Code:
    int x;
    
    
    int main(void)
    {
      return(0);
    }
    
    void FooBar(void)
    {
    }
    Here x has file scope. Every function can access x. It is global to the file. However, other file's cannot access x. It is only known in this file.

    Code:
    
    int main(void)
    {
      FooBar();
      return(0);
    }
    
    void FooBar(void)
    {
      int x;
    }
    This x has function scope. It is only known inside of FooBar. When you get back to main() x does not exist and cannot be accessed.

    Code:
    
    int main(void)
    {
      if (condition)
      {  
         int x=0;
      }
     
    return(0);
    }
    This x has block scope. It is only known inside of the {} block.
    Hmm... you're using things in your code I don't quite seem familiar with. what exactly is "(void)"?
    --MrDoomMaster
    The kind of DooM that makes the MooD

  15. #75
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe some code will clear things up:
    Code:
    #include <iostream>
    using namespace std;
    int Global=0;
    int Func1()
    {
    int a=1;
    int b=2;
    return a+b;
    
    }
    
    //a,b now gone
    int Func2()
    {
    return Global++; //can be changed and accessed since it is global 
    }
    
    int Func3(int val)
    {
    Global=val;
    return Global;
    }
    
    int Func4(int& val)
    {
    val=10;
    return val;
    }
    int main()
    {
    int MainVar=25;
    int MainVar2=20;
    cout<<Func1()<<endl;//outputs 3
    cout<<Func2()<<endl;//outputs 0, Global now 1
    cout<<Global<<endl;//outputs 1;
    cout<<Func3(MainVar)<<endl;//outputs 25, Global now 25
    cout<<Global<<endl;//outputs 25
    cout<<Func4(MainVar)<<endl;//outputs 10, MainVar now 10 
    cout<<MainVar<<endl;//outputs 10
    //note MainVar could not be changed without passing it as 
    //a reference, since its scope is within main()
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM