Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to pass the struct by ref

What I have tried:

#include<iostream>
#include<string>
#include<conio.h>	
using namespace std;

struct Bankinfo{
	string name,location,manager_name;
	int bank_id,emp_numbers;
};

Bankinfo getBankinfo(){

	Bankinfo n;
	cout<<"enter name,location,manager name,bank id,employees number"<<endl;
	getline(cin,n.name);
	getline(cin,n.location);
	getline(cin,n.manager_name);
	cin>>n.bank_id;
	cin>>n.emp_numbers;
	return n;
}


void displayBankinfo(Bankinfo *){
	Bankinfo n;
	cout<<n.name<<endl;
	cout<<n.location<<endl;
	cout<<n.manager_name<<endl;
	cout<<n.bank_id<<endl;
	cout<<n.emp_numbers<<endl;
}



int main(){

Bankinfo n;



getBankinfo();
displayBankinfo(&n);
	return 0;

}
Posted
Updated 6-Nov-21 3:41am

1. In C ++, parameters can be passed to a function either by pointers or by references.
Here it was subsequently requested to implement the parameter transfer via pointer.
In C, Pass-by-reference is simulated by passing the address of a variable.
This is referred to as "C style pass-by-reference. (Source: www-cs-students.stanford.edu)
2. The questioner wants a solution with a structure instead of a class.

Unfortunately, the result is not good C ++ code, but rather a mixture of C ++ and old C code.

So here is the result:
C++
typedef struct {
	string name, location, manager_name;
	int bank_id, emp_numbers;
} Bankinfo;

void getBankinfo(Bankinfo* b)
{
	cout << "Name: "; 
	getline(cin, b->name);
	cout << "Location: ";
	getline(cin, b->location); 
	cout << "Manager-Name: ";
	getline(cin, b->manager_name);
	cout << "Bank ID: ";
	cin >> b->bank_id;
	cout << "Employees-Number: ";
	cin >> b->emp_numbers;
}

void displayBankinfo(const Bankinfo *b) {
	cout << b->name << endl;
	cout << b->location << endl;
	cout << b->manager_name << endl;
	cout << b->bank_id << endl;
	cout << b->emp_numbers << endl;
}

int main() 
{
	Bankinfo b;
	getBankinfo(&b);
    cout << "-------------------------------\n";
	displayBankinfo(&b);
	return 0;
}
 
Share this answer
 
At first glance I see several problems. On the one hand, no reference is passed to the subroutine. It should look like this:
C++
void displayBankinfo(const Bankinfo &n) {
	// Bankinfo n;
	cout << n.name << endl;
	cout << n.location << endl;
	cout << n.manager_name << endl;
	cout << n.bank_id << endl;
	cout << n.emp_numbers << endl;
}

Since the values ​​are only output and should not change, I would declare them as const.

The second point is that the structure is created in the first subprogram, but then not assigned in the main program.

C++
int main() 
{
	Bankinfo n;

	n = getBankinfo();  // assign here!
	displayBankinfo(n);
	return 0;
}

Remarks:
1. I would already use the reference from the main program when reading in the values.
2. Is there a good reason to use a structure here instead of a class?
3. Reading in the values ​​seems very error-prone and is also not secured.
 
Share this answer
 
Comments
Member 15420232 5-Nov-21 18:38pm    
1-me too
2-the question wants it with structure
3-also wants the function void displayBankinfo to take (*) as a parameter
is there other solution to display the output using (*) as a parameter?
phil.o 5-Nov-21 20:24pm    
Yes, you have to use the Member Access Operator ->[^]
When you work with a pointer, you cannot use . as member accessor; you have to use -> operator:
struct thing { int value; };
thing* pointerToThing = new thing;
pointerToThing.value = 0; // Will not compile. Wrong operator.
pointerToThing->value = 0; // OK
Member 15420232 5-Nov-21 20:35pm    
you mean like
void displayBankinfo(Bankinfo *n){

cout<<n->name<<endl;
cout<<n-="">location<<endl;
cout<<n-="">manager_name<<endl;
cout<<n-="">bank_id<<endl;
cout<<n-="">emp_numbers<
merano99 5-Nov-21 21:21pm    
If you had said from the beginning what exactly you are allowed to and what not you could have given better answers immediately.

When you have to use * as reference you have to write:

void displayBankinfo(Bankinfo *n) {
cout << n->name << endl;
...
Member 15420232 5-Nov-21 22:14pm    
I tried that but it still gives me a garbage value ):
Because you do not use the parameter of the displayBankinfo function; you did not even give it a name. Instead, you create a brand new Bankinfo instance (which contains garbage since it is unitialized), and display its values. You may be searching for something like
C++
void displayBankinfo(Bankinfo *n){
	// display n's values here. no need to create a new instance, just use the parameter
}
 
Share this answer
 
v2
Comments
Member 15420232 5-Nov-21 18:43pm    
did not work
phil.o 5-Nov-21 20:01pm    
not specific enough as an issue description...
Member 15420232 5-Nov-21 20:40pm    
void displayBankinfo(const Bankinfo *n){

cout<<n->name<<endl;
cout<<n-="">location<<endl;
cout<<n-="">manager_name<<endl;
cout<<n-="">bank_id<<endl;
cout<<n-="">emp_numbers<
phil.o 5-Nov-21 20:52pm    
This is still not an issue description.
You should also write
Bankinfo n = getBankinfo();
in your main method. Right now you create the variable but you do not affect it the result returned by the getBankInfo() method.
Member 15420232 6-Nov-21 9:24am    
I finally did it

all what I had to do is:
1- void displayBankinfo(BANKinfo *n)
2-use the -> operator
3- assign n== getBankinfo()

thank you for helping me
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900