Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:46+00:00 2026-06-17T12:17:46+00:00

struct anup1 { int a; }; void structpass_1(struct anup1 b) // accepting structure {

  • 0
struct anup1 {
    int a;
};
void structpass_1(struct anup1 b) // accepting structure
{
    cout << b.a;
};
void structpass_2(struct anup1& b) // accepting address of a structure
{
    cout << b.a;
};

int main() {
    struct anup1 a2;
    a2.a = 100;
    structpass_1(a2);
    structpass_2(a2);
}

The above code gives same output…whether accepting parameter is struct / address of struct.

Can anyone please explain to me this behavior?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:17:47+00:00Added an answer on June 17, 2026 at 12:17 pm

    In structpass_1 your structure anup1 is passed by value, so a local copy is done and passed to the function.

    Instead, in structpass_2 the structure is passed by reference, i.e. a pointer to the structure instance is passed to the function (you have pointer semantic but value syntax). No local copy of the whole structure is done.

    Note that for a simple structure containing only one integer passing by value or by reference is the same from a performance perspective. But when you have more complex (and bigger) data, passing by reference is more efficient.

    An important difference between the two cases of passing by value vs. passing by reference is that if you modify the structure instance inside the function body, only if the structure is passed by reference the modifications are persistent at the call site. Instead, when you pass the structure by value, since a local copy is done inside the function body, the modifications are lost when the function exits. e.g.:

    void structpass_1(anup1 b) // pass by value
    {
        cout << b.a << '\n';
        b.a++; // modification lost at the call site
    };
    
    void structpass_2(anup1& b) // pass by reference
    {
        cout << b.a << '\n';
        b.a++; // the caller will see the incremented value for b.a
    };
    
    int main() 
    {
        anup1 a2;
        a2.a = 100;
        structpass_1(a2); // will print 100
        structpass_2(a2); // will print 100
        cout << a2.a; // willl print 101 (structure modified by structpass_2)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct B { void (B::*pf)(int, int); // data member B () : pf(&B::foo) {}
struct LeafDataEntry { void *key; int a; }; int main(){ //I want to declare
struct A { int &r; A (int &i) : r(i) {} void foo ()
struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct Div { int i; int j; }; class A { public: A(); Div&
struct A { int a; virtual void print() {} }; struct B : A
struct hostent *lh = gethostbyname(hostname); int socketDescriptor = socket(AF_INET,SOCK_STREAM, 0); sockaddr_in socketInfo; memset(&socketInfo, 0,
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
struct A { enum InnerEnum { X }; A(InnerEnum x) {} }; int main()
struct DummyStruct{ unsigned long long std; int type; }; DummyStruct d; d.std = 100;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.