Thread: Struct pointer initialization

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Struct pointer initialization

    Hello everyone,
    I wrote a piece of code here, which actually works, but I was told to initialize the data structure that holds the data to be filtered and I dont really understand how to do this.
    As you can see
    double* Values; // holds the data to be filtered
    The pointer Values will be used to create a dynamic array, but first I was told to initialize it . So, how do I initialize it?
    I have a function
    void EnterData(TheData& DataIn)
    {
    // initialize the data structure that holds the data to be filtered, including getting
    // the number of data values from the user

    cout << "Please enter the number of data values to be filtered:" << endl;
    cin >> DataIn.Length;
    cout << endl;
    So, I was trying to write something like DataIn.Values = NULL to initialize the structure where data will be held, but it doesnt really work.
    Thank you for any help.
    Here is a full code.
    Code:
    #include <iostream>
    using namespace std;
    
    struct TheData 
    {
      double* Values;  // holds the data to be filtered
      unsigned long Length;  // number of data values
      bool Valid;   // true if the data values have been Valid by the user
    };
    
    void EnterData(TheData&);
    
    
    int main()
    {
    	int a;
    	TheData OriginalData = {0,0,false};
    	EnterData(OriginalData);
    	cout << endl;
    	//cout << OriginalData.Length;
    	cin >> a;
    	delete [] OriginalData.Values;
    	
    }
    // Allow the user to enter the data to be filtered
    // Arguments:
    //   (1) the structure containing the input data
    // Returns: nothing
    // 
    void EnterData(TheData& DataIn)
    {  
    	// initialize the data structure that holds the data to be filtered, including getting
    	// the number of data values from the user
    
    	cout << "Please enter the number of data values to be filtered:" << endl;
    	cin >> DataIn.Length;
    	cout << endl;
    
    	// allocate memory to the data
    	DataIn.Values = new(double[DataIn.Length]);
    
    	// obtain all of the data values
    	cout << "Please enter the data values to be filtered:" << endl;
    	for (unsigned int i=0; i<DataIn.Length; i++)
    		{
    			cout << "Please enter value #" << i+1 << ": ";
    			cin >> DataIn.Values[i];
    		}
    	DataIn.Valid = true;
    	cout << "All the values were saved successfully.";
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pe4enka
    The pointer Values will be used to create a dynamic array, but first I was told to initialize it . So, how do I initialize it?
    You did it correctly on this line, by initialising the member variable to be a null pointer:
    Code:
    TheData OriginalData = {0,0,false};
    By the way, it is more common to write this:
    Code:
    DataIn.Values = new(double[DataIn.Length]);
    as:
    Code:
    DataIn.Values = new double[DataIn.Length];
    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
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Yes, but the task tells to initialize Values again in the function. I dont really understand why and how as well as what for. but this is the task given. I need to do it in the place, where I colored with green So, is there any way? because if I say, create another pointer like
    double **testpointer and write
    testpointer = &DataIn.Values to check the address of DataIn.Values, then testpointer will point to some memory location, where DataIn.Values is being kept and this address appears to be not zero... How do I make this pointer DataIn.Values to point nowhere, until I allocate memory for DataIn.Values using new?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pe4enka
    Yes, but the task tells to initialize Values again in the function.
    That is impossible. By definition, you can only initialise once. You could simply fail to initialise OriginalData in the main function and then assign an initial value to OriginalData in the EnterData function, yet even then technically that would not be initialisation.

    Quote Originally Posted by pe4enka
    because if I say, create another pointer like
    double **testpointer and write
    testpointer = &DataIn.Values to check the address of DataIn.Values, then testpointer will point to some memory location, where DataIn.Values is being kept and this address appears to be not zero...
    That is expected. The pointer itself has an address, regardless of whether it is a null pointer or if it points somewhere.

    Quote Originally Posted by pe4enka
    How do I make this pointer DataIn.Values to point nowhere, until I allocate memory for DataIn.Values using new?
    You already did that correctly by initialising it to be a null pointer.
    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

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thank you for your help. I guess I will leave just DataIn.Values = NULL, because I can't read my lectruer's mind lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 PM