[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...differences between the declarations: ...

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

Question by mattryan
Submitted on 5/30/2004
Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List)
Rating: Not yet rated Rate this question: Vote
what are the differences between the declarations:

char*const host = "mail.mailserver.com"

and

const char *host = "mail.mailserver.com"


Answer by soodacur
Submitted on 10/29/2004
Rating: Not yet rated Rate this answer: Vote
in the first declaration->
host is an unsigned constant that contains
a pointer to a character array.
in the second declaration->
host is an unsigned integer that contains
a pointer to a character array.
the difference here is that in the first case
the value that host contains can't be changed
but the contents of the location to which it
points can be changed while in the second case ,the value of host can be changed but
the contents of the location to which it
points can not be changed.

 

Answer by harish Kamalapurkar
Submitted on 11/9/2004
Rating: Not yet rated Rate this answer: Vote
char *const host = "mail.mailserver.com"

This declares the pointer host to be constant and not the string itself.

i.e *host = 'r' will work
but host = "harish" will give an error since const value cannot be modified.

const char *host = "mail.maiserver.com";

here the string i.e characters are constant and the string is not

host = "harish" will work
but *host = 'r' will not.

 

Answer by sheena
Submitted on 5/17/2005
Rating: Not yet rated Rate this answer: Vote
In First statement host is a pointer which is const and point to char. i.e in first declaration you can not change the address of host but u can change contents of host.

whereas in case of second statement, it declare host as pointer to char that happens to be const. i.e u can change the address of host but cannot change contents of host.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.