1 |
On Wednesday, August 05, 2015 6:18:07 AM Franz Fellner wrote: |
2 |
> walt wrote: |
3 |
> > On Tue, 04 Aug 2015 08:19:37 +0200 |
4 |
> > Franz Fellner <alpine.art.de@×××××.com> wrote: |
5 |
> > |
6 |
> > > Fernando Rodriguez wrote: |
7 |
> > > > On Monday, August 03, 2015 6:41:22 PM walt wrote: |
8 |
> > > > > That line declares *hostname as a constant and then the statement |
9 |
> > > > > below proceeds to assign a value to the 'constant'. I wonder how |
10 |
> > > > > many hours of frustration have been suffered by student |
11 |
> > > > > programmers while trying to understand the logic behind that. |
12 |
> > > > |
13 |
> > > > Because it's not a constant, it's a pointer-to-constant :) |
14 |
> > > Both of you are right, you can read the declaration in both ways: |
15 |
> > > hostname is of type "pointer to const char". |
16 |
> > > *hostname is of type "const char". |
17 |
> > > |
18 |
> > > But in this case it is not "*hostname", that get's a value assigned, |
19 |
> > > it's simply "hostname". If you do not set hostname to NULL it stays |
20 |
> > > uninitialised, which means its value is what the actual memory is set |
21 |
> > > to - quite undefined. Correct initialization is really important and |
22 |
> > > should be done consequently so it gets an automatism ;) (would avoid |
23 |
> > > issues like this) |
24 |
> > > |
25 |
> > > > |
26 |
> > > > const char *hostname; /* pointer to constant char */ |
27 |
> > > > char *const hostname; /* constant pointer to char */ |
28 |
> > > > const char *const hostname; /* constant pointer to constant char */ |
29 |
> > > > |
30 |
> > > > Is that confusing enough? |
31 |
> > |
32 |
> > confusing++ |
33 |
> > |
34 |
> > Thank you both for being patient enough to teach the ineducable :) |
35 |
> > |
36 |
> > Let me give you one more example of syntax that I find unreasonable, |
37 |
> > and then I'll ask my *real* question, about which I hope you will have |
38 |
> > opinions. |
39 |
> > |
40 |
> > Okay, the statement I referred to above uses this notation: |
41 |
> > |
42 |
> > if (!link->network->hostname) <this notation makes sense to me> |
43 |
> > r = sd_dhcp_lease_get_hostname(lease, &hostname); <this doesn't> |
44 |
> |
45 |
> The "&"-operator returns the address of the object, in this case of |
46 |
hostname. |
47 |
> If you would just pass "hostname" the function would receive a _copy_ of the |
48 |
object. |
49 |
> hostname is an "out-argument", the function writes to it. That is needed |
50 |
sometimes |
51 |
> as C only can return one value, if you need to return more things you need |
52 |
to pass |
53 |
> them as out-args. But for that to work you need to operate on the actual |
54 |
object and |
55 |
> not a copy of it, so you need to pass the address to the actual object. |
56 |
> The declaration of the function of course needs to specify the arg as |
57 |
"pointer to" |
58 |
> the actual type, here "pointer to a pointer to char". |
59 |
|
60 |
You can look at it like that, but more technically it's because C doesn't |
61 |
support out arguments, or reference arguments, or objects. All arguments are |
62 |
passed by value. You can return multiple values in a struct but it's not very |
63 |
convenient both in terms of usability (you need to store the result in a |
64 |
variable before you can use it unless you only care about one member) and |
65 |
performance since everything needs to be copied. Plus the implementation may |
66 |
vary significantly between compilers and architectures |
67 |
|
68 |
So in order to get a value back from the function (other than the return) you |
69 |
pass the address (a pointer) where you want that data to be written. Things |
70 |
like that make C seem primitive if your coming from a higher level language |
71 |
but it is what makes C so powerful. Once you get the hang of it and understand |
72 |
how everything works it's actually simpler than higher level languages because |
73 |
C doesn't do stuff behind you back (or does very little) so you can read C code |
74 |
a understand what's going on under the hood. Most Java and .NET developers for |
75 |
example have no clue about what goes on in their own programs under the hood. |
76 |
|
77 |
> > |
78 |
> > In this context does '&hostname' mean a-pointer-to-a-pointer-to-the- |
79 |
> > charstring we actually need? |
80 |
> > |
81 |
> > Doesn't this code seem needlessly complicated? |
82 |
> > |
83 |
> > <okay, screed over, thanks for listening> |
84 |
> > |
85 |
> > Somewhere I read that there was really only *one* java program ever |
86 |
> > written, and every subsequent java program was written by cut-and-paste |
87 |
> > from the first one. |
88 |
> > |
89 |
> > Is that how professional developers learn the art of programming? |
90 |
|
91 |
That's how you write bugs :) There's nothing wrong with it if you take the |
92 |
take to understand what it's doing but it's too often done blindly. |
93 |
|
94 |
> > I really would like to hear your opinions on that question because I |
95 |
> > feel it's an important topic. |
96 |
> > |
97 |
> > Thanks guys. |
98 |
> > |
99 |
> > |
100 |
> > |
101 |
> |
102 |
> |
103 |
> |
104 |
|
105 |
-- |
106 |
Fernando Rodriguez |