Here is a collection of calls that prints the different consequences of calling.
Note how address and value behave after passing them to functions and when the functions return. Depending on your compiler references may not be supported since they are not ansi-c.
#include <stdio.h>
void AddByValue(int i)
{
printf("%8s: address: 0x%X value: %i\n", "Val A", &i, i);
i = i + 1;
printf("%8s: address: 0x%X value: %i\n", "Val B", &i, i);
}
void AddByPointer(int *iptr)
{
//note that you copy the pointer itself by value
//the value of the pointer is the address of a place in memory
//using the asterix the compiler can navigate to that memory address
//using the typeinfo the compiler knows what is supposed to be at that place in memory
printf("%8s: address: 0x%X value: 0x%X\n", "Point A", &iptr, iptr);
printf("%8s: address: 0x%X value: %i\n", "Point B", iptr, *iptr);
(*iptr) = (*iptr) + 1;
printf("%8s: address: 0x%X value: %i\n", "Point C", iptr, *iptr);
}
void AddByReference(int &i)
{
printf("%8s: address: 0x%X value: %i\n", "Ref A", &i, i);
i = i + 1;
printf("%8s: address: 0x%X value: %i\n", "Ref B", &i, i);
}
struct test
{
int x, y, z;
};
void PrintStrruct(test t)
{
printf("%8s: value: %i\n", "test x", t.x);
printf("%8s: value: %i\n", "test y", t.y);
printf("%8s: value: %i\n", "test z", t.z);
}
void PassingAStruct(test *testptr)
{
(*testptr).x = 0;
testptr->y = 0;
test & ref = *testptr;
ref.z = 0;
}
int main()
{
int i = 1;
printf("%8s: address: 0x%X value: %i\n", "main A", &i, i);
AddByValue(i);
printf("%8s: address: 0x%X value: %i\n", "main B", &i, i);
AddByPointer(&i);
printf("%8s: address: 0x%X value: %i\n", "main C", &i, i);
AddByReference(i);
printf("%8s: address: 0x%X value: %i\n", "main D", &i, i);
printf("--- structs ---\n");
test mytest;
PrintStrruct(mytest);
PassingAStruct(&mytest);
PrintStrruct(mytest);
AddByValue(mytest.x);
PrintStrruct(mytest);
AddByPointer(&mytest.y);
PrintStrruct(mytest);
AddByReference(mytest.z);
PrintStrruct(mytest);
return 0;
}