How do I pass structure to function with pointers and reference ?

How do I pass structure to function with pointers and reference ?
is this a right code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>

using namespace std;

void PrintRaj(struct Raj *R);

struct Raj
{
    int Number=10;

};

int main()
{
    struct Raj *R1;
   struct Raj pR;
    R1=&pR;
    PrintRaj(&pR);
    return 0;
}
void PrintRaj(struct Raj *R)
{
    cout<<R->Number;
}

Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct YourStruct
{
  // some data members
};

// Using pointer
void YourFunc(YourStruct* s)
{
  // use s
}

// Using reference
void YourFunc(YourStruct& s)
{
  // use s
}

int main()
{
  YourStruct ys;

  // call func with pointer
  YourFunc(&ys);

  // call func with reference
  YourFunc(ys);
}
Topic archived. No new replies allowed.