Re: static aray question?
aaaaaaaaahhhhhhhh that's what you guys meant by " it was passed by
value"!!!!!
what I should of said was the object that is pointed to by obj_PASSCODE
pointer is passed by reference, and that obj_PASSCODE pointer is passed by
value.
They are two different things, the pointer and the object that's being
pointed to qualify as two seperate ways that they can be passed as.
So let me reassure myself if I may!
So this is why when we do,
Option#1
================
void f(PASSCODE *r, int u)
{r->TOUCHES = u}
int main
{
int o;
PASSCODE e;
PASSCODE *y = &e;
f(y, 47);
o = y->TOUCHES;
}
==================
then I am passing the "Y" pointer by value and it can't be modified. But the
object's data it points to (PASSCODE e) can be modified in the function and
will be visible in main. And therefore this is why "o" will equal to 47.
Also when I was doing:
Option#2
==================
void f(PASSCODE *r, int u)
{
r = malloc (sizeof (PASSCODE);
r->TOUCHES = u
}
int main
{
int o;
PASSCODE e;
PASSCODE *y = &e;
f(y, 47);
o = y->TOUCHES;
}
====================
the "y" pointer is again passed by value but this time we assign it an
address returned from malloc and we will be able to assign u to a PASSCODE
element (TOUCHES) in the function but because the "y" pointer is a copy,
when we return from the function everything we assigned in reference to "y"
in the function is lost and this is why this time in main, "o" contains junk.
So in order to not loose the "y" pointer and have access to the data TOUCHES
was set as in the function, the previous code sample had to be modified so to
return the pointer like this:
Option#3
================
PASSCODE *f(PASSCODE *r, int u)
{
r = malloc (sizeof (PASSCODE);
r->TOUCHES = u
return r;
}
int main
{
int o;
PASSCODE e;
PASSCODE *y = &e;
y = f(y, 47);
o = y->TOUCHES;
}
====================
and if I wanted to do it so that I don't have to return the pointer but
still have the data of TOUCHES visible in main which was set in the function,
I would pass the "y" pointer by reference. But since a reference to a pointer
is actually the address of the pointer, then the function has to take it as a
double pointer (**). And this would be done this way:
Option #4
================
PASSCODE *f(PASSCODE **r, int u)
{
*r = malloc (sizeof (PASSCODE);
(*r)->TOUCHES = u
return *r;
}
int main
{
int o;
PASSCODE e;
PASSCODE *y = &e;
y = f(&y, 47);
o = y->TOUCHES;
}
====================
But a question arises, in the above code sample, if the "y" pointer is
passed by reference, what is the object pointed to by "y" pointer passed in
as, reference to a reference?????? or I guess by reference too... I don't
know!
However, my MCU compiler has trouble compiling double indirection (**) and
therefore I will have to do it like I showed it above as Option#3.
Igor, in reference to option #3, is very similar to the innital post in this
topic, recall,
Why do you pass a parameter to the function if you never use its initial
value? Just declare h as a local variable, rather than a parameter.
Therefore, doing it as option #3 would be better since I assign something to
the pointer. In VC++ if I do this:
=========================
PASSCODE *f(PASSCODE *r, int u)
{
r = malloc (sizeof (PASSCODE);
r->TOUCHES = u
return r;
}
int main
{
int o;
//PASSCODE e;
PASSCODE *y; // = &e;
y = f(y, 47);
o = y->TOUCHES;
}
====================
VC++ gives the following warning:
c:\_dts_programming\c_programming\c\my_new_tests\tests_withstructures\test.c(39) : warning C4700: local variable 'y' used without having been initialized
and when I run it, it breaks at the calling function, but if I click on
"continue" it allows me to continue the code execution. All this to say,
that, not innitializing the "y" pointer in main and then just passing it like
that is, if I may use the word "unethical" ! So basically as you said it, it
isn't right. The reason I did this though, is that the MCU compiler did not
give any warnings on this ????
--
Best regards
Roberto
"Igor Tandetnik" wrote:
Robby <Robby@discussions.microsoft.com> wrote:
===================
Q#1)
Since the ULC_PASSC_config_passcode() function receives the PASSCODE
obj_PASSCODE pointer by reference
No it doesn't. What makes you think so?
(I guess its by reference and not
by value right?)
Wrong
===============================
Okay, so then we all agree that I was passing it by value .....RIGHT??
This depends on what you mean by "it" here. You were passing PASSCODE*
pointer by value, which also means you were passing PASSCODE object
itself by reference (in the sense the word "reference" is colloquially
used when discussing C programs, where it is pretty much synonymous with
"pointer").
AND
When we pass something by value, its been said over and over again,
that the object or variable is copied into the function, and that any
changes to this variable or object's data will not reflect or be
visible in main or the calling function... RIGHT?
Right. But remember - you were passing a pointer by value, which means
you couldn't change the pointer to point to something else (or set it to
NULL). But you were passing a pointed-to object by reference, meaning
you _could_ change said object through the pointer.
AND
In my previous topic posts, I was told on several occasions that when
we want to pass something by reference, C absolutely requires the "&"
(address of) in front of the variable or object that is being passed
into a function
Consider two equivalent sequences:
void f(PASSCODE*);
PASSCODE pass;
f(&pass); // pass is passed by reference
PASSCODE* p = &pass;
f(p); // pass is still passed by reference. p is passed by value.
It doesn't matter whether you take an address right when calling the
function, or some time before that (and store it in a variable).
Here's an example of passing by reference that doesn't involve an
ampersand:
PASSCODE* p = malloc(sizeof(PASSCODE));
f(p);
free(p);
Here, too, p is passed by value, but the (unnamed) PASSCODE object it
points to is passed by reference.
But in your example you don't put an "&" in front of pPass and you
claim that the data modified in the function will be visible in main
or the calling function?
Changes to pPass are not visible to the caller. Changes to (*pPass) are.
Is it because since pPass is a pointer and when we pass it, it is
automatically understood that its contents is the address >>>>
replacing the need for the "&" ????
Yes, it's understood that a pointer (in most implementations) constains
an address. I don't see where "replacing the need for &" part comes
from - it doesn't make any sense to me.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925