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? > > -- > Fernando Rodriguez >