Thread: variable is being used without initialized

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    34

    variable is being used without initialized

    I get an error when i try to compile this code. I tried to allocate memory in main function and that works. But why it doesn't work in function? I think that there is something wrong with function argument, but not sure.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    
    struct Word
    {
        char letter[ 50 ];
    };
    
    
    void ReadFromFile( const char* filename, Word* dictionary, int& n )
    {
        ifstream fin( filename );
        fin >> n;
        dictionary = new Word[ n ];
        for( int i=0; i<n; i++ )
        {
            fin >> dictionary[ i ].letter ;
        }
        fin.close();
    }
    
    
    
    
    int main()
    {
        Word* dictionary;
        int n;
    
    
        ReadFromFile( "A.txt", dictionary, n );
        for( int i=0; i<n; i++ )
        {
            cout << dictionary[ i ].letter << endl;
        }
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that the dictionary parameter in ReadFromFile is a pointer to Word, so when you write:
    Code:
    dictionary = new Word[ n ];
    what happens is that you're changing the value of this parameter, but this change is not reflected in the caller since pass by value is used. One approach is to use a reference parameter:
    Code:
    void ReadFromFile( const char* filename, Word*& dictionary, int& n )
    That said, I note that you forgot to write a delete[] to match the new[]. You should consider using a container such as std::vector instead of doing manual memory management.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I will also add that you should use std::string, and not char arrays. I will also note that your code is pretty fragile. Change the size of your array and problems may appear (again, use std::string).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-08-2011, 10:56 PM
  2. the variable is being used without being initialized c
    By bob_999 in forum C Programming
    Replies: 7
    Last Post: 01-09-2011, 04:38 AM
  3. how do I know a variable is not initialized?
    By patiobarbecue in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2009, 06:49 AM
  4. Undeclared/un-initialized variable
    By jadedreality in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2007, 11:16 AM
  5. Constant Being Initialized AFTER Variable That Uses It
    By mercury529 in forum C++ Programming
    Replies: 22
    Last Post: 07-30-2006, 09:51 AM