passing table rows by address

This is a discussion on passing table rows by address within the C++ Programming forums, part of the General Programming Boards category; I know how to pass structures by using pointers to functions. Is it possible to pass a table or a ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    passing table rows by address

    I know how to pass structures by using pointers to functions. Is it possible to pass a table or a particular row from a table by address using pointers?

    say i have something like

    Code:
    STRUCT_CODE lavalamp [25];
    and i want to move it to the function. what would the prototype and function header lines look like? How would it look like in the main module when the function is called?

    also, what if you made a function that took a member out of lavalamp (which is of type STRUCT_CODE) called beads and multiplied it by two and returned the member changed?
    cout << "\aBIOTCH";

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "what would the prototype and function header lines look like?"

    void function(STRUCT_CODE* p)

    Function prototypes and function headers in a definition can be the exact same thing, and it's probably good practice to make them the same.

    "How would it look like in the main module when the function is called?"

    Code:
    int main()
    {
        STRUCT_CODE lavalamp[25];
        function(lavalamp); //array names act like pointers
        return 0;
    }
    "what if you made a function that took a member out of lavalamp (which is of type STRUCT_CODE) called beads and multiplied it by two and returned the member changed?"


    Ok, suppose you did that, now what is the question?
    Last edited by 7stud; 04-26-2003 at 08:54 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    excerpt from program

    Code:
    #define TABLE_SIZE 25
    
    struct STUDENT_STRUCT
    	{
    		char last_name [21];
    		char first_name [21];
    		char social [12];
    		char phone [9];
    		int quiz [5];
    		int final;
    		int final_grade;
    	};
    
    STUDENT_STRUCT  user_prompt		(STUDENT_STRUCT*);
    int				grade_computer	(STUDENT_STRUCT*);
    int				quiz_computer	(STUDENT_STRUCT*);
    
    int main ()
    {
    	
    	
    	STUDENT_STRUCT	main_struct[TABLE_SIZE];
    	int row = 0;
    	int count = 1;
    	char response[8];
    	
    
    
    	cout << "Welcome, Dr. Jacobs, to the marketing 101 grade "
    		<< "computation program.  (hit any key when ready)\n";
    	cin.get();
    	cin.ignore(80, '\n');
    	cout << "\nWould you like to enter your first student's "
    		<< "information now?  Enter Y or N\n";
    	cin.getline (response, 11);
    	cin.ignore(80, '\n');
    
    	while (toupper(*response) == 'Y')
    	{
    		while (row < TABLE_SIZE)
    		{
    			user_prompt(&main_struct[row]);
    			++row;
    			++count;
    			cout << "Enter another student? Enter Y or N";
    			cin.getline (response, 11);
    			cin.ignore(80, '\n');
    		}
    	
    	}
    
    	cout << "Final student class averages are as follows: \n\n";
    	row = 0;
    	
    	for (row=0;row<count;++row)
    	{
    		main_struct[row].final_grade = grade_computer(main_struct[row]);
    		main_struct[row].quiz = quiz_computer(main_struct[row]);
    	}
    
    	return 0;
    }
    
    STUDENT_STRUCT user_prompt (STUDENT_STRUCT* stud_ptr)
    {
    	int x;
    	
    
    	
    	cout << "\nEnter student's last name: ";
    	cin.getline (stud_ptr.last_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's first name: ";
    	cin.getline (stud_ptr.first_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's social security number: ";
    	cin.getline (stud_ptr.social, 12);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's phone number: ";
    	cin.getline (stud_ptr.phone, 9);
    	cin.ignore (80, '\n');
    	for (x=0;x<5;++x)
    	{
    		cout << "\nEnter student's grade for quiz " << x+1 << ": ";
    		cin >> stud_ptr.quiz[x];
    	}
    	cout << "\nEnter the student's final exam grade: ";
    	cin >> stud_ptr.final;
    	return stud_ptr;
    }
    in the called function, i move the address of main_struct into the student function and then take it in as a pointer called stud_ptr...

    am i going about this the wrong way? Does it make sense, in main to just send over one row at a time and then increment the counters?
    cout << "\aBIOTCH";

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    hmm

    you see how in user_prompt function I collect all those strings like last name, etc. ?

    those pointers, i.e.(cin.getline (stud_ptr.first_name, 21), kinda messing with my head.
    can i mess with the members of stud_ptr in that function? cuz i know that pointers just point to the first byte of each string...
    cout << "\aBIOTCH";

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    cin.ignore(80, '\n');
    	cout << "\nWould you like to enter your first student's "
    		<< "information now?  Enter Y or N\n";
    	cin.getline (response, 11);
    	cin.ignore(80, '\n');
    
    	while (toupper(*response) == 'Y')
    	{
    		while (row < TABLE_SIZE)
    		{
    			user_prompt(&main_struct[row]);
    			++row;
    			++count;
    			cout << "Enter another student? Enter Y or N";
    			cin.getline (response, 11);
    			cin.ignore(80, '\n');
    		}
    	
    	}
    
    	cout << "Final student class averages are as follows: \n\n";
    	row = 0;
    	
    	for (row=0;row<count;++row)
    	{
    		main_struct[row].final_grade = grade_computer(main_struct[row]);
    		main_struct[row].quiz = quiz_computer(main_struct[row]);
    	}
    This should probably all be in the form of a do-while loop. Did you consider what would happen if the user chose N to start off?

    "Does it make sense, in main to just send over one row at a time and then increment the counters?"

    I don't think so. I would send the array name as the argument to the function, and inside the function prompt for the input and fill in the data. One function call will be more efficient than up to 25 function calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 07:56 AM
  2. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  3. import address table (IAT)
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 07:01 AM
  4. Passing an address to a function
    By Vicious in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2004, 12:05 PM
  5. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM

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