* or & ?

Hi,

my most confusing part of programming in c++ is the decission of using * or & in a functions argument-type when passing data pointers.

I have read that e.g. foo(char* x) would make a copy of the pointer which is mostly used to just use the content of a char array. But I have an example in which x is global and is initialized with new values and later on displayed. So there must have been forwarded the adress of the variable, as foo(char& x) would actualy do. Thats what confuses me. What should be the difference between * and & in this case?

Confusing scenario:

foo.cpp
foo(char* const data)
{
data[0] = 1;
data[1] = 2;
...
}

main.cpp
char x[4];
foo(x);
print(x)
Output: 1,2,..

??
In foo(), data is a const pointer to memory that contains data of type char. data[0] access the contents of the memory pointed to by data, data[1] accesses data + 1. So the memory pointed to by data is set to 1, the memory pointed to by data + 1 is set to 2.

in main(), x is c-style array. When x is passed to foo, it is 'degraded' to a pointer to memory (the memory used for x array). This memory location is data in foo(). Hence the changes specified in foo() affect the memory used by c-array x. Hence the contents of x are now 1 2 ? ? where ? is unknown as not set by the code.

In pass-by-value, a clone is made and passed into the function. The caller's copy cannot be modified.
In pass-by-reference, a pointer is passed into the function. The caller's copy could be modified inside the function.
In pass-by-reference with reference arguments, you use the variable name as the argument.
In pass-by-reference with pointer arguments, you need to use &varName (an address) as the argument.



Play around with passing by value, by address and by pointer is to play around and check out the differences like in the following and see how each has a different effect.

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
#include <iostream>

void function_by_value(int aNumber)
{
    std::cout << "Inside function_by_value a) " << aNumber << '\n';
    
    aNumber += 27;
    std::cout << "Inside function_by_vale b) " << aNumber << '\n';
}

void function_by_reference(int &aNumber)
{
    aNumber += 27;
    std::cout << "Inside function_by_reference " << aNumber << '\n';
}

void function_by_pointer(int* aNumber)
{
    std::cout << "Inside function_by_pointer a) " << aNumber << '\n';
    
    aNumber += 27;
    std::cout << "Inside function_by_pointer b) " << aNumber << '\n';
}

int main()
{

    int number{7};
    
    std::cout << "In main " << number << '\n';
    function_by_reference(number);
    std::cout << "In main " << number << "\n\n";
    
    number = 7;
    std::cout << "In main " << number << '\n';
    function_by_pointer(&number);
    std::cout << "In main " << number << "\n\n";
    
    number = 7;
    std::cout << "In main " << number << '\n';
    function_by_value(number);
    std::cout << "In main " << number << "\n\n";
    
    return 0;
}


Lots of stuff here
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html


Topic archived. No new replies allowed.