What is this way called?

Hello there !
so I have a little question, I cannot really differ between the calling by value and calling by reference, in some question I was asked to write a program that reads a positive integer number and calculate the sum of all values from 1 to N, I made the first part and it worked, then the question asked to use functions in writing the code and I did that part and it's working as well, so I know about three ways of writing functions, but I just need confirmation of what is this way called, and an example of the others, THANK YOU!

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
#include<iostream>
using namespace std;
int read(int *);
float calc(int , float *, float );
void print(int );

int read(int *N)
{
		cout<<"Please enter a postivie integer number"<<endl;
	cin>>*N;
	while(*N<0)
	{
		cout<<"The number is negative,please enter another value."<<endl;
		cin>>*N;
	}
	return *N;
}

 float calc(int N, float *sum, float count)
{
	for(count=1;count<=N;count++)
	{
		
		*sum=*sum+count;
	}
	return *sum;
}


void print(int N,float sum)
{

	cout<<"The sum of"<<N<<" is "<<sum<<endl;
}

void main()
{
	int N;
	float sum,count;
	sum=0;
	count=1;
	read(&N);
	calc(N,&sum,count);

	print(N,sum);

system("pause");
}
Using an asterisk, *, is referred to as passing by pointer. Using an ampersand, &, is referred to as by passing by referrence. And using no symbol is referred to as passing by value.

Passing by value is the same as if you used the actual value of the variable: "Hello", 5, 'a'; and then the function has a variable name to correspond with that value and do math to it.

Passing by reference is like as if that variable actually existed within that function. The function can modify the value of the variable anyway you choose fit, and once the function returns, that variable is assign that value.

Passing by pointer is passing the memory location of that object. I'm a little rusty on this section, and I believe it works very similar to the way that by referrence works.
Passing by pointer is passing the memory location of that object. I'm a little rusty on this section, and I believe it works very similar to the way that by referrence works.


As I recall this is one of the few sparks of the religious wars around here :) I consider passing-by-pointer to be passing-by-value, where the object passed happens to be a pointer - the function gets a copy of the pointer, and anything done to that copy does not happen to the original pointer.

Cue the war... now!
Cue the war... now!

Here we go.

I agree that the pointer itself is a copy of the pointer passed, but operations done on that pointer after dereferencing (spelling?) have effect on the actual value from the calling function. But, any pointer arithmetic doesn't effect the original pointer.

I'll call it half and half :)
Pfft. Only operations done on what you get by dereferencing that pointer, and that's not interfering with the pointer at all. You could do the same thing (hideously) by passing the memory address as a plain int.

FIGHT FIGHT FIGHT FIGHT!
You could do the same thing (hideously) by passing the memory address as a plain int


How would you go about dereferencing an int?

Let's say object A is at location 0xff08ac10, with pointer to that. So the value of the pointer is also 0xff08ac10.

Now let's pass int that equals 0xff08ac10. What now?
I'm reading up on the pointer tutorial and I haven't really read anything yet that helps ME understand it any better. I understand the memory usage by variables, but between by value and by referrence, I haven't found a need for by pointer. Ugh, here goes another way to do the same thing I was already doing.
You can change what a pointer points to, a reference is static. You can't change it.
Now let's pass int that equals 0xff08ac10. What now?


Hold on, I'm drinking. Let me make something compile...

I wrote this. I used long because on my system, a pointer is 8 bytes, and a long is 8 bytes, and it means I don't have to take something of different byte size and mash it.


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
#include <iostream>
using namespace std;

void someFunction (long x)
{
  cout << "Someone gave me the long value " << x << endl; 
  long* y = (long*)x;
  cout << "At that memory location is the long value " << *y << endl;
  
  // Bob's your uncle. Do what you like with the pointer.
}

int main()
{
  long john_silver = 17;
  long* bob = &john_silver;

  cout << "address of the long is " << bob << endl;
  cout << "the long is " << john_silver << endl;

  cout << "Size of a long* is " << sizeof(long*) << endl;
  cout << "Size of a long is " << sizeof(long) << endl;
  cout << "Must be same to work" << endl;

  // get integer value of the address
  
  long catsOnIce = (long)bob;

  cout << "value of catsOnIce, which is a long, NOT a pointer, is " << catsOnIce << endl;

  someFunction (catsOnIce);
}


This, ladies and gentlemen, is one of the reason why C (and C++) phreaking rocks. It's YOUR data, and you can do WHATEVER you like with it. Now please excuse me whilst I organise another trip to North Korea. I hear that this year might be the last Arirang and I really don't want to miss it.
Last edited on
Topic archived. No new replies allowed.