Scope

This is a discussion on Scope within the C Programming forums, part of the General Programming Boards category; I understand now. In this first program the vector s_name is defined at the begining of main, therefore it's scope ...

  1. #1
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742

    Scope

    I understand now. In this first program the vector s_name is defined at the begining of main, therefore it's scope is the whole function or in this case, the whole program.
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    #define MAX 4
    int main()
    {
    	vector<string> s_name;
    	char s_array[MAX][15] = {"Govtcheez", "Zen", "Witch_King", "Stoned_Coder"};
    	for(int i=0;i < MAX; i++)
    	{
    		s_name.push_back(s_array[i]);
    	}
    	for(vector<string>::iterator ii = s_name.begin(); ii < s_name.end();++ii)
    		cout << *ii << endl;
    	return 0;
    }
    However in this function, the vector s_name is defined inside of the loop and it is destroyed each loop cycle, therefore it is undefined when the program tries to access it outside the for loop. This second program will not compile.
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    #define MAX 4
    int main()
    {
    
    	char s_array[MAX][15] = {"Govtcheez", "Zen", "Witch_King", "Stoned_Coder"};
    	for(int i=0;i < MAX; i++)
    	{
    		vector<string> s_name;
    		s_name.push_back(s_array[i]);
    	}
    	for(vector<string>::iterator ii = s_name.begin(); ii < s_name.end();++ii)
    		cout << *ii << endl;
    	return 0;
    }
    I remember Salem mentioning this before but I didn't quite understand at the time.
    I compile code with:
    Visual Studio.NET beta2

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    when something is declared within a block its scope is that block!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    That's God damn right. And I might have known it sooner had I paid more attention and taken up C++ earlier.
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I would like to thank YOU, O Witch_King, for my first C++ Lesson!
    (I know this applies in C as well, of course, but all the same it was a good example...)

    I have a question? What are things you can not do in C++ with C?

    That is, how far can the two be merged?

    (ie: Can you use "printf()" if you so wish?)

    Also, would anyone care to give an overview of the C++ libraries (ie: <vector>, <list>, <string>...)

    And what does this "< >" mean?

    I do realize there are tutorials out there, but I'm just trying to stimulate a little conversation, that's all...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    C and C++ have something called 'link' compatability. Don't ask me to explain because I am too burdened by College and learning C++ at this time. I want to understand things but for now this is just something that is true that I was able to read without understanding it.

    The brackets <> are for specifying what type of container you want to store. Mind you I am very tired at the moment, I might not have said that perfectly. Example:
    vector<string>
    vector<int>
    list<int>
    list<Student_t>

    cout << "replaces printf";

    When you study C++ you will learn more about C than you would get out of reading a C textbook, unless that is your first or second C textbook and you don't know anything yet.

    Pick up a C++ text and take a look.
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    for(vector<string>::iterator ii = s_name.begin(); ii < s_name.end();++ii)
          cout << *ii << endl;
    should be
    Code:
    for (vector<string>::const_iterator ii = s_name.begin(); ii != s_name.end(); ++i)
          cout << *ii << endl;
    In most cases
    Code:
    ii < s_name.end()
    will work I think but not always, I threw on a const_iterator
    but it's not really needed here.

    I'm suprised though
    Code:
    s_name.push_back(s_array[i]);
    VC 6.0 used to choke on this code right? Must
    have added support in the new addition.

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    In the book they use '!=' instead of '<' but I used the '<' because I'm used to 'C', for example in for loops. Not that I don't believe you but could you think of an example where '<' wouldn't work?

    Yes, VS.NET has not given me any problems thus far with the code from 'Accelerated C++' by Koenig, yet I had some difficultites when I tried running the same code in VC++6
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM
  5. Question about C# scope rules
    By converge in forum C# Programming
    Replies: 3
    Last Post: 01-30-2002, 05:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21