1 |
Fernando Rodriguez wrote: |
2 |
> On Monday, August 03, 2015 6:41:22 PM walt wrote: |
3 |
> > That line declares *hostname as a constant and then the statement below |
4 |
> > proceeds to assign a value to the 'constant'. I wonder how many hours |
5 |
> > of frustration have been suffered by student programmers while trying to |
6 |
> > understand the logic behind that. |
7 |
> |
8 |
> Because it's not a constant, it's a pointer-to-constant :) |
9 |
Both of you are right, you can read the declaration in both ways: |
10 |
hostname is of type "pointer to const char". |
11 |
*hostname is of type "const char". |
12 |
|
13 |
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. |
14 |
Correct initialization is really important and should be done consequently so it gets an automatism ;) (would avoid issues like this) |
15 |
|
16 |
> |
17 |
> const char *hostname; /* pointer to constant char */ |
18 |
> char *const hostname; /* constant pointer to char */ |
19 |
> const char *const hostname; /* constant pointer to constant char */ |
20 |
> |
21 |
> Is that confusing enough? |
22 |
> |
23 |
> -- |
24 |
> Fernando Rodriguez |
25 |
> |