On Wednesday, August 05, 2015 6:18:07 AM Franz Fellner wrote: > 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".
You can look at it like that, but more technically it's because C doesn't support out arguments, or reference arguments, or objects. All arguments are passed by value. You can return multiple values in a struct but it's not very convenient both in terms of usability (you need to store the result in a variable before you can use it unless you only care about one member) and performance since everything needs to be copied. Plus the implementation may vary significantly between compilers and architectures So in order to get a value back from the function (other than the return) you pass the address (a pointer) where you want that data to be written. Things like that make C seem primitive if your coming from a higher level language but it is what makes C so powerful. Once you get the hang of it and understand how everything works it's actually simpler than higher level languages because C doesn't do stuff behind you back (or does very little) so you can read C code a understand what's going on under the hood. Most Java and .NET developers for example have no clue about what goes on in their own programs under the hood. > > > > 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? That's how you write bugs :) There's nothing wrong with it if you take the take to understand what it's doing but it's too often done blindly. > > I really would like to hear your opinions on that question because I > > feel it's an important topic. > > > > Thanks guys. > > > > > > > > > -- Fernando Rodriguez