Passing a pointer...question

I'm trying to pass a structure pointer to a class function. I want to pass the pointer in such a way that the changes made in the class function will be kept/"permanent". I know that with regular variables I can pass by reference. I also know that I can just do stuff in the function and return that pointer but I'd like to avoid returning as much as possible. Here are the two scenarios...

1. Pass by reference...but how do it pass a pointer by reference?

2. I see people pass a pointer by pointer...how does it work? are the changes done with pointer passed by pointer permanent?

3. There's no need to pass by reference/pointer.


I'll try to exemplify. Note that C++ is a superset of C, so any C techniques may be used in C++.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//The basic structure for the examples:
struct _MyStruct
{
    int myInt;
    double myDouble;
};

//A typedef is required so I don't need to repeat the "struct" keyword for C:
typedef struct _MyStruct MyStruct;

//From C:  Pass by value:
void ChangeData(MyStruct ms)
{
    ms.myInt = 10;
    ms.myDobule = 3.14;
}
...
MyStruct ms;
ms.myInt = 0;
ms.myDouble = 6.022045e23;
ChangeData(ms);
//Examine ms now.  It remains unchanged after the function call.

//In C, if you wanted to change the value of the original variable you had to use pointers:
void ChangeData(MyStruct *ms)
{
    ms->myInt = 10;
    ms->myDouble = 3.14;
}

...
MyStruct ms;
ms.myInt = 0;
ms.myDouble = 6.022045e23;
ChangeData(&ms); //Note the Address Of operator here.
//Examine ms now.  The values changed.

//C++ adds the ability to pass variables by reference to avoid pointer notation:

void ChangeData(MyStruct &ms)
{
    ms.myInt = 10;
    ms.myDobule = 3.14;
}
...
MyStruct ms;
ms.myInt = 0;
ms.myDouble = 6.022045e23;
ChangeData(ms);
//Examine ms now.  Just like sample 2, the values changed. 


That should clarify the basic scenarios. Now you say you want to pass a pointer by reference? That's possible too, but is it what you want, or did the previous samples already gave you what you needed?
closed account (D80DSL3A)
no reply = nevermind.
Last edited on
closed account (4z0M4iN6)
2. I see people pass a pointer by pointer...how does it work? are the changes done with pointer passed by pointer permanent?


You have a structure:

1
2
3
4
struct MyStruct {
      int a;
      int b
};


You declare a structure:

MyStruct MyStructObject;

You have a pointer - no, no need.

you have a function, whether obj.function or only function - not interesting now.

1
2
3
4
5
void function( MyStruct * ptr)
{
     ptr->a = 1;
     ptr->b = 2;
};


And then you call such a function:

function( &MyStructObject);


Would the changes be permanent?


Depends - how you understand "permanent".

I think, if you switch off your Commputer and the dada are not saved, it will not be permanent. And also your hard disc will only last some years - no it depends of the life time of your variable MyStructObject, whether this is a local or global variable.

-----------


If you pass by reference, you would write your function this way:

1
2
3
4
5
void function( MyStruct & ref)
{
     ref.a = 1;
     ref.b = 2;
};


And you would call your function this way: function( MyStructObject);

And what is the difference between pointer and reference? Some say much, some say not much.

We could say, pointer and reference are the same, only the compiler make them look differently. A reference is a pointer, which adress cou can't change and which the compiler let look like a variable. Could be some programmers couldn't unterstand pointers but understood variables - no that wasn't the only reason, the programmers can also not change the address, which means they cannot make so much nonsense. Oh maybe shouldn't have blabbed out the secret of the references. And now you know, what it means, when others say, you should take references - this he will understand and he cannot make much nonsense.


3. There's no need to pass by reference/pointer.


Depends, how many data - on performance - on stack ressources. You could write your function as:

1
2
3
4
5
6
MyStruct function( MyStruct data)
{
     data.a = 1;
     data.b = 2;
     return data;
};


And you could call your function so:

MyStructObject = function(MyStructObject);

This you could do and it makes not much difference for such a tiny structure object. But if it's a big structure object, you should consider, all the data are copied to the stack, when the function is called and all the data are copied to the stack, when the function returns and all the data are then copied to your structure object.

This means bad performance and a soon full stack. When you use a reference or pointer, there is only the address on the stack and you modify the data direct.
Last edited on
Topic archived. No new replies allowed.