Pointer and function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Pointer and function

 
0
  #1
Jan 3rd, 2006
Can somebody please tell what does this function prototype indicate--

  1. const char* fun( char const *,const char*);

Is it some thing like that the return type would always be constant char ? And what is the difference in the two declarations-- char const* and const char*?

Thanks
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer and function

 
0
  #2
Jan 3rd, 2006
Originally Posted by aminura
Can somebody please tell what does this function prototype indicate--
  1. const char* fun( char const *,const char*);
Is it some thing like that the return type would always be constant char ?
It may be more helpful to read const as "read-only". You the programmer are not supposed to be writing to it. So the function returns a pointer to read-only data (likely a C-style string).
Originally Posted by aminura
And what is the difference in the two declarations-- char const* and const char*?
Personal preference.

char const* - what the pointer is pointing to is read-only.
const char* - what the pointer is pointing to is read-only.

char *const - the pointer is read-only (may not be incremented, etc.).

const char *const - the pointer is read-only (may not be incremented, etc.); what the pointer is pointing to is read-only.
char const *const - the pointer is read-only (may not be incremented, etc.); what the pointer is pointing to is read-only.
Last edited by Dave Sinkula; Jan 3rd, 2006 at 4:53 pm. Reason: Yay, Dave. Let's try to get it right the first time.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Pointer and function

 
0
  #3
Jan 4th, 2006
Originally Posted by Dave Sinkula
It may be more helpful to read const as "read-only". You the programmer are not supposed to be writing to it. So the function returns a pointer to read-only data (likely a C-style string).
Thanks for the reply.
I guess I haven't got this thing fully into my lazy brain! So does it actually mean that whatever the arguments maybe , the output can never be manipulated or I'll be always having the same output.
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer and function

 
0
  #4
Jan 4th, 2006
It generally means you shouldn't change what is pointed to.
  1. #include <stdio.h>
  2.  
  3. const char *foo(const char *text)
  4. {
  5. puts(text);
  6. return text;
  7. }
  8.  
  9. int main(void)
  10. {
  11. const char literal[] = "non-modifiable string literal";
  12. char *oops = foo(literal);
  13. oops[0] = 'N'; /* BAD! */
  14. return 0;
  15. }
Here the a C++ compiler will prevent you from assigning the return value of foo to oops. But a C compiler may just give you a warning, allowing you to do bad things. Namely, a string literal may be put in program code (or somewhere else that is read-only), and should not be considered to be writable. But the languages do afford you ways to do things you shouldn't.

By const-qualifying the return value, the programmer who gave you the protoype is providing a damn good hint to prevent you from doing things you ought not do. Warnings and errors should then hopefully guide you into writing correct code.

But there is generally no foolproof way of stopping a programmer from doing dumb things and writing code that crashes or otherwise does bad things.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Pointer and function

 
0
  #5
Jan 4th, 2006
Ok.Thank you!
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC