walt wrote:
> On Tue, 04 Aug 2015 08:19:37 +0200
> Franz Fellner <alpine.art...@gmail.com> wrote:
> 
> > Fernando Rodriguez wrote:
> > > On Monday, August 03, 2015 6:41:22 PM walt wrote:
> > > > That line declares *hostname as a constant and then the statement
> > > > below proceeds to assign a value to the 'constant'.  I wonder how
> > > > many hours of frustration have been suffered by student
> > > > programmers while trying to understand the logic behind that.
> > > 
> > > Because it's not a constant, it's a pointer-to-constant :)
> > Both of you are right, you can read the declaration in both ways:
> > hostname is of type "pointer to const char".
> > *hostname is of type "const char".
> > 
> > But in this case it is not "*hostname", that get's a value assigned,
> > it's simply "hostname". If you do not set hostname to NULL it stays
> > uninitialised, which means its value is what the actual memory is set
> > to - quite undefined. Correct initialization is really important and
> > should be done consequently so it gets an automatism ;) (would avoid
> > issues like this)
> > 
> > > 
> > > const char *hostname; /* pointer to constant char */
> > > char *const hostname; /* constant pointer to char */
> > > const char *const hostname; /* constant pointer to constant char */
> > > 
> > > Is that confusing enough?
> 
> confusing++
> 
> Thank you both for being patient enough to teach the ineducable :)
> 
> Let me give you one more example of syntax that I find unreasonable,
> and then I'll ask my *real* question, about which I hope you will have
> opinions.
> 
> Okay, the statement I referred to above uses this notation:
> 
>  if (!link->network->hostname)      <this notation makes sense to me>
>  r = sd_dhcp_lease_get_hostname(lease, &hostname);     <this doesn't>

The "&"-operator returns the address of the object, in this case of hostname.
If you would just pass "hostname" the function would receive a _copy_ of the 
object.
hostname is an "out-argument", the function writes to it. That is needed 
sometimes
as C only can return one value, if you need to return more things you need to 
pass
them as out-args. But for that to work you need to operate on the actual object 
and
not a copy of it, so you need to pass the address to the actual object.
The declaration of the function of course needs to specify the arg as "pointer 
to"
the actual type, here "pointer to a pointer to char".

> 
> In this context does '&hostname' mean a-pointer-to-a-pointer-to-the-
> charstring we actually need?
> 
> Doesn't this code seem needlessly complicated?
> 
> <okay, screed over, thanks for listening>
> 
> Somewhere I read that there was really only *one* java program ever
> written, and every subsequent java program was written by cut-and-paste
> from the first one.
> 
> Is that how professional developers learn the art of programming?
> 
> I really would like to hear your opinions on that question because I
> feel it's an important topic.
> 
> Thanks guys.
> 
> 
> 



Reply via email to