• C

how is this example dereferencing

A is modified using dereferencing

I can understand that getting address of A and getting value of that address is dereferencing

But this example is calling a function that mutates A

how is this dereferencing
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
>> how is this dereferencing
To help experts who would like to address this question, could you clarify your question. Explain what you think should happen; what is actually happening; and where you are surprised.

FYI - wish you had used different variable names for A in main and in your function, since one is an array and the other is a pointer - makes it a little easier to talk about A.
0
ozoCommented:
A[ i ] is the same as *(A+i)
* is the dereference operator
0
phoffricCommented:
>> so strings in C are nothing
Is this another question or your assertion? What do you mean by this statement?
0
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

rgb192Author Commented:
so strings in C are nothing
this is a screenshot of a youtube tutorial

Explain what you think should happen; what is actually happening; and where you are surprised.
No expectations about output

A[ i ] is the same as *(A+i)
* is the dereference operator
I understand how * is 'derefencing'

I am asking if calling a function is also called 'refrerencing'
0
phoffricCommented:
>> But this example is calling a function that mutates A
>> I am asking if calling a function is also called 'refrerencing'
I am not sure I understand what you are asking. Maybe this will help.

All functions parameters are passed by value. That is, a copy of the caller's argument is placed on the function's stack.

In main(), you pass A (array) to Double(). In this case, what is passed is not the entire array; instead what is passed is a pointer to the first element in the array.

Even if you were to change A within Double, it would not affect the A array in main. In Double(), you are modifying what A (pointer) is pointing to.

ozo explained the dereferencing part in Double.
0
phoffricCommented:
Given an array, A, you already know that A[k] is the kth element of A.
Given a pointer, A, then if the pointer happens to be pointing to an array element (not necessarily the first element), then A[k] is the kth element beyond what is pointed to by the pointer A provided that the kth element exists.
0
rgb192Author Commented:
All functions parameters are passed by value. That is, a copy of the caller's argument is placed on the function's stack.
So pass by value is passing all the array elements
and pass by reference is passing the first

What is the difference in programming (*)?

Even if you were to change A within Double, it would not affect the A array in main. In Double(), you are modifying what A (pointer) is pointing to.
But all the values are doubled in main

Given an array, A, you already know that A[k] is the kth element of A.
Given a pointer, A, then if the pointer happens to be pointing to an array element (not necessarily the first element), then A[k] is the kth element beyond what is pointed to by the pointer A provided that the kth element exists.
Are you trying to say something plus 1
0
käµfm³d 👽Commented:
So pass by value is passing all the array elements...
Imagine how slow programs would be if that were the case.
0
ozoCommented:
The value that is being passed is A, which is the address of the array.
0
phoffricCommented:
>> and pass by reference is passing the first
Maybe you heard the term "pass by reference" by coming across some C++ articles? What I wrote earlier was:
All functions parameters are passed by value. That is, a copy of the caller's argument is placed on the function's stack.
This is true for C.
0
rgb192Author Commented:
in c:
passing c[20]
pass by value means: address of the first element of array c[0]?
0
ozoCommented:
In c, if you pass c[20] to a function, you are passing the value of c[20]
If you pass &c[0] to a function, you are passing the value of &c[0]
the value of &c[0]  is the address of the first element of array c
the value of c is also  the address of the first element of array c
0
rgb192Author Commented:
I thought I was only passing the address of the first element of the array and not all the values of the array (too much memory in bytes)
if I call function with parameter c
then function only knows address of c[0] the address of first element
0
ozoCommented:
You are passing (A,size)
The value of A is the address of the first element of the array.
The value of size is sizeof(A)/sizeof(A[0]) which is the number of elements in A
0
käµfm³d 👽Commented:
C is a much more "basic" language than languages like Java, C#, Python, or PHP. With arrays, you don't get an implicit length they way you do in Java. This means you have to keep track of the array length yourself. This is why your function has a size parameter.

As you were told above, passing the array passes the address to the first element in the array. So, knowing that you receive the address of the first element of the array, and you also receive the size of the array (as a 2nd parameter), can you think of how your function can access the remaining elements of the array? (Hint:  Read up on pointer arithmetic.)
0
rgb192Author Commented:
You are passing (A,size)
The value of A is the address of the first element of the array.
The value of size is sizeof(A)/sizeof(A[0]) which is the number of elements in A
I understand what you have wrote.
But I do not understand this:
should be pass by reference because it is first address of array.
Address=reference

can you think of how your function can access the remaining elements of the array?
add datatype size to original address foreach element
intArray[0]+4=intArray[1]
intArray[1]+4=intArray[2]
intArray[2]+4=intArray[3]
intArray[3]+4=intArray[4]
0
ozoCommented:
the term "pass by reference" has a different conventional meaning,
so you will confuse people if you refer to passing an address in C as pass by reference.
0
ozoCommented:
intArray[0]+4=intArray[1]
Incorrect.

&intArray[0]+1 == &intArray[1]
&intArray[0]+4 == &intArray[4]
Assuming sizeof(intArray[0])==4
((char *)&intArray[0])+4 == (char *)&intArray[1]
0
rgb192Author Commented:
from
http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

#include<stdio.h>
#include<stdlib.h>
void Modify(int p, int * q, int * o)
{
	p = 27; // passed by value - only the local parameter is modified
	*q = 27; // passed by value or reference, check call site to determine which
	*o = 27; // passed by value or reference, check call site to determine which
}
int main()
{
	int a = 1;
	int b = 1;
	int x = 1;
	int * c = &x;
	Modify(a, &b, c);   // a is passed by value, b is passed by reference by creating a pointer,
	// c is a pointer passed by value
	// b and x are changed
	return(0);
}

Open in new window



passed by value or reference, check call site to determine which
so the call to function determines call by value/reference
Modify(a, &b, c)
not the formal parameters in the function
void Modify(int p, int * q, int * o)
0
ozoCommented:
Technically, those are all call by value, but call by reference is simulated by using a pointer for the value.
0
rgb192Author Commented:
Technically, those are all call by value, but call by reference is simulated by using a pointer for the value.
in actual parameters
Modify(a, &b, c)
or formal parameters
void Modify(int p, int * q, int * o)

Or do you mean by defining the variable?
Which datatype?
int* test
int &test
int test
 Inside or outside the function?
0
ozoCommented:
When you call Modify(a, &b, c)
you pass the value of a, the value of &b, and the value of c.
All you can ever pass to a function in C are values, regardless of of how the function is declared.
(this differs somewhat from C++)
0
rgb192Author Commented:
I know of dereferencing as printing a variable value
print *value
print value


so


Derefencing by using a function:
So dereferencing is just getting the value of a variable
a
&b
c
0
ozoCommented:
In C, a pointer type may be derived from a function type, an object type, or an incomplete
type, called the referenced type. A pointer type describes an object whose value
provides a reference to an entity of the referenced type. A pointer type derived from
the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a
pointer type from a referenced type is called ‘‘pointer type derivation’’.
And the operation of the unary * operator is sometimes referred to as "dereferencing".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
complete answer about creating a reference and then dereferencing

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.