Click here to Skip to main content
15,891,810 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
for example I have this one;
struct students {
   string name;
   int ID;
   char Grade;
};

students undergraduates,graduates;


I want to pass it to a function what to do? thanks :)
Posted
Updated 1-Mar-22 18:31pm
v3
Comments
Albert Holguin 1-May-11 16:36pm    
edit: added pre tags

Here is how:

C++
struct Student { //use singular, not plural
   //...
}

Student first, second; 

void PassByValue(Student who); //prototype
void PassByReference(Student& who); //prototype

PassByValue(first); //you work with a shallow copy of "who, if you changes is, actual object remains the same
PassByReference(second); //no copy (faster), when you modify who you modify actual passed object


Call is identical, but second call always needs a variable.
Structure or not — no matter, can be any type.

—SA
 
Share this answer
 
v2
Comments
Albert Holguin 1-May-11 17:24pm    
good answer
Sergey Alexandrovich Kryukov 1-May-11 17:25pm    
Thank you, Albert.
--SA
Albert Holguin 1-May-11 17:26pm    
I answered that one hastily :)
Sergey Alexandrovich Kryukov 1-May-11 17:46pm    
:-)
Legor 3-May-11 7:16am    
Shouldn't your call to PassByReference() look like PassByReference(&first)?
C#
typedef struct _STUDENT
{
   string    name;
   int      ID;
   char      Grade;
} STUDENT;
void fnStruct(STUDENT student)
{
  // you can everything you want
  // the value from caller is not affected
}
void fnStructRef(STUDENT& student)
{
  // you can read or write values
  // the value from the caller can be modified
  // usually a ref cannot be a null pointer
}
void fnStructConstRef(const STUDENT& student)
{
  // you can only read the members
  STUDENT*  writable = (STUDENT*)&student;
  // this construct makes it possible to full access
  // the value from the caller will be modified
}
void fnStructPtr(STUDENT* student)
{
  // you can read or write values
  // the value from the caller can be modified
  // the pointer can be a null pointer
  if(student)
  {
    // the param is not null
  }
}
void fnStructConstPtr(const STUDENT* student)
{
  // you can only read the members
  // the pointer can be a null pointer
  if(student)
  {
    // the param is not null
    STUDENT*  writable = (STUDENT*)student;
    // this construct makes it possible to full access
    // the value from the caller will be modified
  }
}
void main()
{
  STUDENT   undergraduates,
            graduates;
  // parameter cannot be changed in any circumstances
  fnStruct(undergraduates);
  // parameter can be changed
  fnStructRef(undergraduates);
  // parameter should not be changed, but it can
  fnStructConstRef(undergraduates);
  // parameter can be changed
  fnStructPtr(&undergraduates);
  // parameter should not be changed, but it can
  fnStructConstPtr(&undergraduates);
}

Regards.
 
Share this answer
 
Comments
Niklas L 2-May-11 11:16am    
Not really necessary to give away how to get rid of const-ness, it's there for a reason, but 5 nonetheless.
Legor 3-May-11 7:14am    
Good answer, besides that its a bit too complex for this simple question imo.
Stefan_Lang 4-May-11 3:16am    
I agree with Niklas, you should not cast away const! If you do so, your API lies: it declares that it treats the object you pass as reference as a const, i. e. it will not change it. So anyone calling that function will rely on the assumption that the object will not be changed, and will consequently get unexpected results if it still does.

P.S.: In the very rare case that you really must cast away const, you should use const_cast instead. But, to be honest, I cannot think of a single example where you can do this safely.
mbue 4-May-11 10:15am    
Cast operations are one of the fundamental operations in c. They exist a long time before microsoft invented their placebo cast keywords. The principle is that the programmer can cast anything to anything else. Therefore the programmer has to know what he is doing with a cast. That you should not cast a const value to a non const value isnt a evidence that this cannot be happen. Ive written this only for completeness and i never wrote that anybody should do that. But it can happen and a programmer should know that.
Thanks for attention.
Stefan_Lang 4-May-11 12:06pm    
Well, if you agree that *normally* a cast is not neccessary, then I see no point in putting that into the example code. That is all I wanted to point out.

Besides, const_cast and the other *_cast keywords are not a MS invention, they are part of the ANSI C++ standard.

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