944,087 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3358
  • C++ RSS
Jan 3rd, 2006
0

Pointer and function

Expand Post »
Can somebody please tell what does this function prototype indicate--

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
aminura is offline Offline
47 posts
since Oct 2004
Jan 3rd, 2006
0

Re: Pointer and function

Quote originally posted by aminura ...
Can somebody please tell what does this function prototype indicate--
C++ Syntax (Toggle Plain Text)
  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).
Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 4th, 2006
0

Re: Pointer and function

Quote 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.
Reputation Points: 10
Solved Threads: 0
Light Poster
aminura is offline Offline
47 posts
since Oct 2004
Jan 4th, 2006
0

Re: Pointer and function

It generally means you shouldn't change what is pointed to.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 4th, 2006
0

Re: Pointer and function

Ok.Thank you!
Reputation Points: 10
Solved Threads: 0
Light Poster
aminura is offline Offline
47 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inheritance in C???
Next Thread in C++ Forum Timeline: Half Life 2 SDK; Coding help wanted





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC